A parameter or argument is data which is taken as input or considered as additional information by the function for further processing. There are two types of parameters or arguments. The parameters which are passed in the function call are known as actual parameters or actual arguments. The parameters which are received by the called function are known as formal parameters or formal arguments. Example is shown below:
In the above example, x and y are known as actual parameters and a and bare known as formal parameters. In the above code, we can see that the return type of the function add is void. This is a keyword in C. The voidkeyword is a datatype. If the function does not return any value, we specify the data type void. Generally void means nothing / no value.
Note: The formal parameters and actual parameters can have the same names i.e., if the actual parameters are x and y, then the formal parameters can also be x and y. But, it is recommended to use different names for actual and formal parameters.
Classification of Functions
Based on the parameters and return values, functions can be categorized into four types. They are:
- Function without arguments and without return value.
- Function without arguments and with return value.
- Function with arguments and with return value.
- Function with arguments and without return value.
Function without arguments and without return value
In this type of functions there are no parameters/arguments in the function definition and the function does not return any value back to the calling function. Generally, these types of functions are used to perform housekeeping tasks such as printing some characters etc.
Example:
1
2
3
4
5
6
7
8
9
|
void printstars( )
{
int i;
for(i = 0; i < 20; i++)
{
printf(“ * ”);
}
return;
}
|
In the above example, printstars function does not have any parameters. Its task is to print 20 stars whenever it is called in a program. Also printstarsfunction does not return any value back.
Function without arguments and with return value
In this type of functions, the function definition does not contain arguments. But the function returns a value back to the point at which it was called. An example for this type of function is given below:
Example:
1
2
3
4
5
6
7
|
int readint( )
{
int num;
printf(“Enter a number: “);
scanf(“%d”,&num);
return num;
}
|
In the above example, readint function has no parameters/arguments. The task of this function is to read a integer from the keyboard and return back to the point at which the function was called.
Function with arguments and with return value
In this type of functions, the function definition consists of parameters/arguments. Also, these functions returns a value back to the point at which the function was called. These types of functions are the most frequently used in programming. An example for this type of function can be seen below:
Example:
1
2
3
4
5
6
|
int add(int x, int y)
{
int result;
result = x + y;
return result;
}
|
In the above example, the function add consists of two arguments or parameters x and y. The function adds both x and yand returns that value stored in the local variable result back to the point at which the function was called.
Predefined / Library Functions
A function is said to be a predefined function or library function, if they are already declared and defined by another developer. These predefined functions will be available in the library header files. So, if we want to use a predefined function, we have to include the respective header file in our program. For example, if we want to use printf function in our program, we have to include the stdio.h header file, as the function printf has been declared inside it.