In this tutorial, you will learn about functions in c programming and the types of functions in c programming.
A function is a single comprehensive unit (self-contained block) containing a block of code that performs a specific task.
This means function performs the same task when called which avoids the need of rewriting the same code again and again.
C functions are classified into two categories:
Library functions are built-in standard function to perform a certain task. These functions are defined in the header file which needs to be included in the program.
Click here to learn about standard library math function.
For examples: printf()
, scanf()
, gets()
, puts()
etc… are standard library functions.
The user defined functions are written by a programmer at the time of writing the program. When the function is called, the execution of the program is shifted to the first statement of called function.
[adsense1]
/* program to calculate the area of square */
#include <stdio.h>
void area(); //function prototype
int main() //function main begins program execution
{
area(); //function call
return 0;
} // end main
void area() //called function
{
int square_area,square_side;
printf("Enter the side of square :");
scanf("%d",&square_side);
square_area = square_side * square_side;
printf("Area of Square = %d",square_area);
} //end function area
Explanation:
In the above program, function area
is invoked or called in the main
function. The execution of program now shifts to called function area
which calculates the area of square. The void
in funtion prototype indicates that this function does not return a value.
return_value_type function_name (parameter_list) { definitions statements }
The function_name is an identifier.