Functions in C Programming

Function in C: The block of code or some logic wrapped inside curly braces ({ }) that performs a specific operation. We already saw some C functions, which you may not have noticed them.

For instance, printf(), scanf(), strcpy() etc. are some of the built-in functions in C programming language. The following are the advantages of functions in C Programming

  1. It will help us to divide the large programs into small groups. So, we can understand and debug the program quicker and better.
  2. Multiple persons can work on the same program by assigning different functions to each of them.
  3. Functions in C prevent us from writing the same logic multiple times.
  4. Functions help us to call the same function over and over.

Syntax of a Functions in C Programming

The basic syntax of the Functions in C Programming is as shown below:

Return_Type Function_Name (Parameters)
{
  Local Variable Declaration;

  Logic;

  Executable Statement 1;

   ……
}
  • Return_Type: When the function called, the Function may or may not return value.
    1. If the function in C returns any value, then we have to replace the Return_Type with the appropriate data type. For example, int, float, char, etc., and also use the return keyword to return the value when we call the function.
    2. If the function does not return any value, then we have to replace the Return_Type with the void keyword. No return keyword is needed.
  • Function_Name: It can be any name you wish to give other than the system reserved keywords.
  • Parameters: Every function accepts 0 or more parameters; it entirely depends upon the user requirements. While declaring the parameters don’t forget the appropriate data type. For example (int a, int b)
  • Local Variable Declaration: Sometimes, we may need some temporary variable, which is required only for this particular function, then we can declare them inside the function. It is not mandatory, purely depends upon user requirements. Remember, these variables are available to this particular function only; we can’t access them outside this function.
  • Logic: Any mathematical or any code or calculations you want to implement in this particular function.
  • Executable Statement: Any printf statements to print some data from this particular function.

Type of Functions in C Programming

There are two types of functions in C Programming language:

  • Library Functions: All the built-in functions supported by the C Language called a Library function. We don’t have to bother about the logic inside the Library functions in C because of those functions stored in header files. In our previous articles, We used many library functions such as printf(), scanf(), sqrt(), strcpy() etc.
  • User Defined Functions: Instead of relying only on built-in functions, C language allows us to create our own functions called as user defined functions. For example, if we want to calculate the standard deviation or some mathematical calculations, then we can place them in separate functions with the proper function name. Then we can call that function multiple times.

C User Defined Functions implementation

To implement the user defined function in C program, we have to follow a few rules such as:

C Function Declaration

It will inform the compiler about the return type, function name, and the number of arguments along with the data types.

Return_Type Function_Name (Parameters)

For example,

int Add (int, int)

The Function declaration is optional. As we all know, C programming starts executing from the main () function. So, when the compiler reaches the function call, then it will travel upwards to check for the function. It means,

  • If we placed the Function Definition before the main () function, then there is no need to declare the function in C.
  • If we placed the Function Definition after the main() function then, it is mandatory to declare the function because it will inform the compiler

Calling the Function in C Programming

Nothing but calling the original function with a valid number of arguments and valid data type. For example, Add (2, 3)

NOTE: User defined function name should exactly match with the calling function in C Programming.

C Function Definition

It is the place where we are going to put all the logics, calculations, etc. We can place the function definition either Before the main () function. Or After the main () function in C programming.

For example,

int Add (int a, int b)
{
  int Sum;
  Sum = a + b;
  return sum;
}

NOTE: Please don’t forget the return keyword if you defined the function other than the void return type.

Find Sum and Average of 3 Numbers using Functions in C Programming

In this function program, the user asked to enter three numbers. Then by calling the function, we will calculate the Sum and Average of that three numbers.

/* Example for Functions in C Programming */
#include <stdio.h>

// Function Declaration
void Average ( float, float, float );                               

int main( )               
{
  float a, b, c;
  int x = 4, y= 6, z =5;
  
  printf ("\nPlease Enter 3 Number to find Sum & Average \n");
  scanf ( "%f %f %f", &a, &b, &c ) ;
  
  Average (a, b, c);  
  Average (x, y, z);                     
}

void Average ( float x, float y, float z)   
{
  float Sum, Average;
  
  Sum = x + y + z;
  Average = Sum/3;
  
  printf ("\n Sum of %.2f, %.2f and %.2f = %.2f", x, y, z, Sum );
  printf ("\n Average of %.2f, %.2f and %.2f = %.2f \n", x, y, z, Average);

}
 Functions In C Programming

It is a C function declaration. If you forget this function declaration, the compiler will throw an error.

void Average ( float, float, float );

The below statement will ask the user to enter 3 numbers

printf ("\n Please Enter 3 Number to find Sum & Average \n");

The below statement will store the user input values in a, b, c variables

scanf ( "%f %f %f", &a, &b, &c ) ;

In the next line, we called the function.

Average (a, b, c);

When the compiler reaches this function, it will traverse to the top to check for the Average () function. If the function fails to identify the function with the Average name, then it will throw an error.

In this case, while traversing upwards, it will stop at

void Average ( float, float, float );

The above function declaration will take the compiler to the below function

void Average ( float x, float y, float z );

First, this function will check for the arguments, and it will only execute if

  • Number of arguments passing to the function is equal to the declared function arguments
  • Data types of the arguments passing to the function are equal to the declared function arguments
Function Definition

Within the C function, we declared 2 local variables Sum and Average. In the next line, we calculated the sum and average of three numbers using Assignment Operators

Sum = x + y + z;

Sum = 10 + 20 + 30 = 60

Average = Sum / 3;

Average = 60 / 3  = 20;

The below printf statements are used to print the sum and average to the output

printf ("\n Sum of %.f, %.f and %.f = %.2f", x, y, z, Sum );

printf ("\n Average of %.2f, %.2f and %.2f = %.2f\n", x, y, z, Average);

In the next line, We called Average () one more time, this time, we passed local variables as function arguments. We called 2 times because it will help you to understand that we can call the function n number of times from other functions.