In this tutorial, we are going to learn about two types of function arguments.
Formal and Actual Arguments:
An argument is an expression that is passed to a function by its caller in order for the function to perform its task. It is an expression in the comma-separated list bound by the parentheses in a function call expression.
A function may be called by the portion of the program with some arguments and these arguments are known as actual arguments (or) original arguments.
Actual arguments are local to the particular function. These variables are placed in the function declaration and function call. These arguments are defined in the calling function.
The parameters are variables defined in the function to receive the arguments. Formal parameters are those parameters that are present in the function definition. Formal parameters are available only within the specified function. Formal parameters belong to the called function.
Formal parameters are also the local variables to the function. So, the formal parameters are occupied memory when the function execution starts and they are destroyed when the function execution completed.
Let us consider the below example:
#include <stdio.h>
int add(int, int);
void main() {
int P = 10, Q = 20;
printf("Sum of two numbers = %d\n", add(P, Q)); // variables P, Q are called actual arguments
}
int add(int A, int B) { // variables A, B are called formal parameters
return(A + B);
}
Output:
Sum of two numbers = 30
In the above code whenever the function call add(P, Q) is made, the execution control is transferred to the function definition of add().
The values of actual arguments a and b are copied into the formal arguments A and B respectively.
The formal parameters A and B are available only within the function definition of add(). After completion of execution of add(), the control is transferred back to the main().
References:
Happy Learning 🙂