Function in C
Function
A block of code written to perform a specific task is called a function. Function in C programming is a reusable block of code that makes a program easier to understand, test and can be easily modified without changing the calling function. Functions divide the code and modularize the program for better and effective results. In short, a larger program is divided into various subprograms, which are called functions.
Advantages of using function
- Increase code reusability
- Program development is faster
- Debugging will be easier
- Reduces complexity
Types of functions
- Library function
It is a predefined function in a header file. The programmer can use this function by simply adding a header file to the program. One cannot change or alter the meaning of this function. Examples:
- printf(), scanf()
- getch(), clrscr()
- pow(), sqrt()
- User-Defined Function
A user-defined function is declared, defined, and used by the programmers according to their requirements. There are 4 different types of user-defined functions:
- Function with arguments and a return value
- Function with arguments and no return value
- Function with no arguments and a return value
- Function with no arguments and no return value
Difference Between:
Library function | User-defined function |
---|---|
Library functions are predefined functions defined in C compiler. | User-defined functions are defined by the user to perform specific tasks. |
It doesn’t need to declare and define the library function to use it. | It needs to declare and define the user-defined function before using it. |
These functions are stored in different header files of the C library. | These functions are not stored in header files and require a function prototype to use them. |
Program execution time is faster. | Program execution time is slower. |
E.g., printf(), scanf(), sqrt(), etc. | E.g., sum(), prime(int a), product(int a, int b), etc. |
Components of Function
- Function declaration (prototype)
Declaration of function informs the compiler about the existence of function and it will be defined and used later.
return_type function_name(parameter-list);
- Function definition
A user-defined function must be defined before it is called or used in the program. It consists of all the code required for its implementation.
return-value-type function-name(parameter-list)
{declarations and statements}
- Function call
After declaring and defining user-defined function, it can be used when needed. Generally, a user-defined function is used by calling it by passing required data.
function_name(argument-list);
- Return statement
One of the main features of the function is that it can return a value to the calling function. In order for a function to return a value to the calling function, the function should have a return statement.
Accessing a Function by passing values
Function with both arguments and a return value.
This type of function has two-way communication.
#include<stdio.h>
#include<conio.h>
int rect_area(int,int);
void main()
{
int l, b, a;
print("Enter the length and breadth: n");
scanf("%d %d", &l, &b);
a= rect_area(l,b);
printf("The area of rectangle is %d", a);
getch();
}
int rect_area(int l, int b)
{
int area;
area= l*b;
return area;
}
Output
Enter the length and breadth:
5
7
The area of rectangle is 35
Function with arguments but no return type
This type of function has one-way communication
#include<stdio.h>
#include<conio.h>
void rect_area(int,int);
void main()
{
int l, b;
printf("Enter the length and breadth: n");
scanf("%d %d", &l, &b);
rect_area(l,b);
getch();
}
void rect_area(int l, int b)
{
int area;
area= l*b;
printf("The area of rectangle is %d",area);
}
Output
Enter the length and breadth:
5
7
The area of rectangle is 35
Function without arguments but with return type.
Here, an arguement is not passed from the calling function, but there is a need for a return statement.
#include<stdio.h>
#include<conio.h>
int rect_area(void);
void main()
{
int a;
a= rect_area();
printf("The area of rectangle is %d", a);
getch();
}
int rect_area(void)
{
int area,l,b;
printf("Enter the length and breadth: \n");
scanf("%d,%d", &l, &b);
area= l*b;
return(area);
}
Output
Enter the length and breadth:
5
7
The area of rectangle is 35
Function without any arguments and return type.
If we do not want a return value, we must use the type void and miss out on the return statement.
#include<stdio.h>
#include<conio.h>
void rect_area();
void main()
{
rect_area();
getch();
}
void rect_area()
{
int l,b, area;
printf("Enter length and breadth: \n");
scanf("%d,%d", &l, &b);
area=l*b;
printf("The area of rectangle is %d",area);
}
Output:
Enter the length and breadth:
5
7
The area of rectangle is 35