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 the C programming language. The following are the advantages of functions.

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

There are two types of functions in the C Programming language:

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

C Programming Functions Syntax

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 is called, it may or may not return a value. If the function in C returns any value, we must 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 it. We must replace the Return_Type with the void keyword if it does not return any value. No return keyword is needed.

  • Function_Name: It can be any name you wish to give other than the system reserved keywords.
  • Parameters: Every method accepts 0 or more parameters depending on 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 method, then we can declare them inside it. It is not mandatory, and it purely depends upon user requirements. Remember, these C variables are only available to this function; we can’t access them outside.
  • Logic: Any mathematical or any code or calculations you want to implement.
  • Executable Statement: Any printf statements to print some data from it.

C User Defined Functions implementation

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

Function Declaration in C Programming

It will inform the C compiler about the return type, function name, number of arguments, and 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 method call, it will travel upwards to check for the definition. It means,

  • If we placed the Definition before the main (), there is no need to declare the function.
  • If we place the Definition after the main(), it is mandatory to declare the method 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 the calling name in Programming.

C Function Definition

It is the place where we are going to put all the logic, calculations, etc. We can place the definition either Before the main () or after the main().

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 method other than the void return type.

C Sum and Average of 3 Numbers using Functions

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

#include <stdio.h>

// 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 void Average ( float, float, float ) declaration, the compiler will throw an error.

The first printf statement will ask the user to enter 3 numbers. And the below statement will store the user input values in a, b, and c variables.

In the next line, we called the Average(a, b, c). When the compiler reaches this point, it will traverse to the top to check for the Average() declaration. If the compiler fails to identify the method with the Average name, it will throw an error.

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

void Average ( float, float, float );

The above declaration will take the compiler to the below method

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

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

  • The number of arguments passing to the function is equal to the declared arguments
  • Data types of the arguments passing to this are equal to the declared arguments

We declared 2 local variables within the C function, 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 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 the Average () function in the C main one more time. This time, we passed local variables as method arguments. We called 2 times because it will help you to understand that we can call the method n number of times from others.