Actual and Formal Parameters
Any variable declared within the parenthesis is referred to as the parameters during the function declaration. Parameters tell the function about the argument datatype, their order, and the number of arguments passed in the function.
In the function to perform the task, the necessary data is sent as parameters.
The parameters are optional i.e., we can have the function without parameters.
Arguments are some actual values during the function call they are passed. There exist a difference between argument and parameters. Arguments are used at the time of calling the function whereas at the time of function declaration and definition parameters are used.
Parameters are of two types they are:
- Actual parameter
- Formal parameter
In a program, the parameters can be actual parameters and formal parameters. Below is the combined example program of argument and parameter.
Program:
#include<stdio.h>
int add(int x,int y); //function declaration
void main(){
int x=10;
int y=20;
int sum = add(x,y);//passing arguments
printf("Sum %d" ,sum);
}
int add(int a, int b){//parameters
int c = a+b;
return c;}
Output:
Sum 30
Actual parameter:
- The simple definition of the actual parameter is parameters that are written in the function call.
- While calling or invoking the function or method the parameters that are passed are called actual parameters.
- Arguments are known as the actual parameters.
- Actual parameters contain the actual value. The change in the value of formal parameters doesn’t reflect on the actual parameters but it is mostly based on the method that we use to pass the parameters.
- Numbers, expressions, or even function calls are used as actual parameters.
- In the actual parameters no need to mention the datatypes. Any number of variables can be passed which are separated by a comma.
- We can pass direct values, expressions, and variables in the actual parameters.
Syntax:
<Function_name>(actual parameters);
Example:
sum(a,b);//function calling
Formal parameter:
- The simple definition of the Formal parameter is Parameter Written in Function Definition is Called “Formal Parameter.
- At the time of function definition or declaration the parameters which are passed to the function are called Formal parameters.
- Parameters are also known as Formal parameters.
- The change in the actual parameters can be reflectedin the Formal parameter this is based on the method that we use to pass the parameters.
- In the Formal parameters we have to mention the datatypes.we can pass any number of variables that are separated by a comma.
- Formal parameters act like a local variable. They are defined within the function or method and exit after the function or method exits. These parameters cannot be accessed out of the function.
Syntax:
<return type><Function_name>(actual parameters);
Example:
Int sum(int x,int y); //function declaration
- Below is the combined example program of actual and Formal parameters.
Program:
#include<stdio.h>
int add(int x,int y); //function declaration
void main(){
int x=5;
int y=10;
int result= mul(x,y);//Actualarguments
printf("Result %d" ,result);
}
//function defination
int mul(int a, int b)// Formal parameters
{
int c = a*b;
return c;
}
Output:
Result 50
From the above example, x and y are actual parameters and they pass the actual values i.e., 5 and 10 respectively. They are in the main method and passed as parameters in the calling function. Here the formal parameters area and b which are passed as parameters in the function definition. Into the formal parameters, the value of actual parameters is copied and a specified function is performed and then returned the value to the calling function.
- As we know that the actual and Formal parameters are different but there exist some similarities between them they are mentioned below:
- Both parameters are related to the function.
- Both are written within the parenthesis.
- We can pass any number of parameters by separating them with a comma.
We have two methods for passing the data into the function they are:
- Call by value.
- Call by reference.
Depending upon the type of the value passed to them as parameters these two methods are differentiated.
Call by value:
- Into the formal parameters in this method the value of actual parameters is copied.
- If any changes are made in the formal parameters do not reflect on the actual parameters because in this method both parameters share different memory addresses.
Example:
sum(a,b);
Program:
#include <stdio.h>
int add(int a){// Formal parameters
a = a+10;
return a;
}
int main(){
int x=5;
add(x);// Actual parameters
printf("The x value is: %d\n", x);
return 0;
}
Output:
The x value is: 5
From the above example, the value of actual parameters x=5has been copied into the formal parameters a. After the function as exit, the value of the actual parameter is the same i.e. x=5.
Call by reference:
- By modifying the formal parameters the value of actual parameters can be changed in this method.
- If any changes are made in the formal parameters that reflect on the actual parameters because in this method both parameters share the same memory address.
Example:
sum(&a,&b);
Program:
#include <stdio.h>
void add(int *a){// Formal parameters
*a = *a+10;
}
int main(){
int x=5;
add(&x);// Actual parameters
printf("Value of x is: %d",x );
return 0;
}
Output:
Value of x is: 15
From the above example, the value of actual parameters x=5 has been copied into the formal parameters a. After the function as exit the value of the actual parameter is modified i.e. x=15.
Value of x is: 15