Functions in C

Function in C is a collection of statements that are written to performs a specific task. All C program has minimum one function, which is main(), and all the other functions are defined in the main function of a C program.

Example of Functions in C

#include <stdio.h>
int max(int a, int b) //function to find max of two numbers
{
if (a > b)
return a;
else
return b;
}

int main(void)
{
int x = 20, y = 30;
int max = max(x, y);
printf("max is %d", max);
return 0;
}

Basically,  functions in C are exactly like the subroutines and functions of Fortran or the procedures and functions of Pascal. You can divide your code into separate functions. The way to divide the code between the different functions is up to you, but logically the division is that each function performs a specific task.

Function Declaration in C

return-type function-name (parameter declarations, if any)

{
declarations
statements
}

In C programming Language if you don’t need to declare a function within the main function if and only if the function has been declared before the C program.

However, if the function is declared after the main function then you have to declare the function in the main function of the C program. Because the function declaration informs the compiler about the name of the function, the type of return and the parameters. The function definition contains the actual content of the function.

The function does not require to return a value and the calling function may ignore the value returned by the function.

Function Definition in C

return-type function-name(parameters)
   {
   declarations
   statements
   return value;
   }

Example:

#include<stdio.h>
int sum(int n1,int n2)
{
return(n1+n2);
}

int main()
{
int num1=11,num2=22;
int result;
result = sum(num1,num2);
printf("\nResult : %d",result);
return(0);
}

It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.

  1. Return Type: The function can return a value. The return type is the data type of the value returned by the function.
  2. Function Name: This is the name of the function. The function name and parameter list together constitute the function reference.
  3. Parameters: The parameter is like a placeholder. After calling the function, you pass the value to the parameter. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and the number of function parameters. The parameters are optional; this means that the function may not contain parameters.
  4. Function Body: The content of a function contains a set of instructions that define its operation.

Parameter Passing to functions

Parameters passed to the function are called actual parameters. For example, in the above program, 20 and 30 are actual parameters.

There are two most common ways to pass parameters:

  1. Pass by value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.
  2. Pass by references: Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in actual parameters of the caller.

Advantage of Function in C Programming Language

There are the following advantages of the functions in C Programming Language.

  • Save time by avoiding rewriting the same logic/code in the program.
  • Call C functions any number of times in the program and from anywhere in the program.
  • Track a large C program when it is divided into many functions.
  • Reusability is a major achievement of the C function.

Please note, function definitions may appear in any order in one or more source files, although no function may be split between files.

If the source program appears in several files, you may have to say more to compile and load it than if everything appears in one, but it’s a matter of the operating system, not the language attribute.

Some Important points about the function in C.

  1. Every C program has a main () function that is called by the operating system when the user starts the program.
  2. Each function has a return type. If the function returns no value, then the void parameter is returned as the type. In addition, if the return type of a function is invalid, we can still use the return statement in the body of the function definition, without providing any constant, variable, etc.
  3.  In C, functions can return any type except arrays and functions. We can work around this restriction by returning a pointer to an array or a pointer to a function.
  4. An empty list of parameters in C means that the parameter list is not specified and the function can be called with any parameters. In C, it’s not a good idea to declare a function like fun (). To declare a function that can only be called without a parameter, we should use “void fun (void)”.

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart