Actual and formal parameters are two different forms of parameters that we use while declaring, defining and invoking a function. The actual parameter is the one that we pass to a function when we invoke it. On the other hand, a formal parameter is one that we pass to a function when we declare and define it.
Actual parameters are the genuine values that a function has to work on. However, the formal parameters are just variables defined to accept the real values on which the function will work. Before exploring more differences between actual and formal parameters let us discuss the term parameter.
What is Parameter in Programming?
A parameter is a way to convey information to a function. Sometimes we also refer to parameters as arguments. Parameters are nothing but the input provided to the function. Function work on these parameters to produce a result or to perform a certain action.
Now, these parameters are passed to a function in two different forms that we refer to as actual parameters and formal parameters. In this section, we are going to discuss the differences between these two types of parameters.
Content: Actual Vs Formal Parameter
- Comparison Chart
- Types of Parameters
- What is an Actual Parameter?
- What is a Formal Parameter?
- Key Differences
- Conclusion
Comparison Chart
Basis for Comparison | Actual Parameter | Formal Parameter |
---|---|---|
Definition | They are actual values passed to a function on which the function will perform operations | They are the variables in the function definition that would receive the values when the function is invoked |
Occurrence | It occurs when we invoke a function | It occurs when we declare and define a function |
Provided by | Either by the programmer or by the user | Only by programmer |
Data types | Data types are not mentioned with the actual parameters | Data types are mentioned along the formal parameters |
Form | It can be a value or a variable | It is always a variable |
Example | add (a, b); | int add (int x, int y) { //body } |
Types of Parameters
Sometimes functions require some data to perform a specific task. So, we provide that data to a function in the form of parameters.
If we talk about a function, it occurs in a program thrice.
- First time when it is declared
- Second when it is defined
- The third time is when it is called
Let’s take an example of a program where we will study three occurrences of a program.
#include<stdio.h> int add (int x, int y); //function declaration Void main(){ int a, b, result; printf(“Enter two values that you want to add”); scanf (“%d %d”, &a, &b); result = add(a, b); // function calling printf(“result of addition is %d”, result); } int add (int x, int y) // function definition { int sum; sum = x + y; return sum; }
You can notice each time a function occurs in a program we have to pass parameters to it but in different forms. So, if we classify parameters on the basis of their form, they are of two types:
- Actual Parameters
- Formal Parameters
We use actual parameters while calling a function. While we use formal parameters for declaring and defining a function. Let us explore more about these parameters in the section ahead.
What is the Actual Parameter?
Actual parameters are the values, variables or expressions that we pass to a calling function. While defining the actual parameters in the calling function we do not associate data type with the parameters.
In a calling function, the actual parameters can be provided either by the programmer at the time of programming or by the user in real-time when the program is being executed.
To understand the actual parameters let us take an example:
In the example below the actual parameters are passed by the user of the program in real-time.
#include<stdio.h> int add (int x, int y) // x and y are the formal parameters { int sum; sum = x + y; return sum; } Void main(){ int a, b, result; printf(“Enter two values that you want to add”); scanf (“%d %d”, &a, &b); result = add(a, b); // a and b are the actual parameters printf(“result of addition is %d”, result); }
In the program below the actual parameters are passed by the program while coding.
#include<stdio.h> int add (int x, int y) // x and y are the formal parameters { int sum; sum = x + y; return sum; } Void main(){ result = add(23, 42); // 23 and 42 are the actual parameters printf(“result of addition is %d”, result); }
What is a Formal Parameter?
Formal parameters are the variable that we pass to function when it is declared or defined in the program. The formal parameters defined during function definition accept the real values in the order they are passed by the calling function. The statements in the function definition access these real values by referring to them with the variable name defined by the formal parameter.
As in the code below you can see that the formal parameter x and y in the function definition of int add(int x, int y) accept the values from the calling function add(a, b). The statements of function definition of add() access the real values with the variable name x and y.
#include<stdio.h> int add (int x, int y) // x and y are formal parameters { int sum; sum = x + y; return sum; } Void main(){ int a, b, result; printf(“Enter two values that you want to add”); scanf (“%d %d”, &a, &b); result = add(a, b); //a and b are actual parameters printf(“result of addition is %d”, result); }
Formal parameters are always passed to a function definition along with their associated data type.
Key Differences Between Actual and Formal Parameters
- Actual parameters are the genuine values that are passed on to the called function. However, the formal parameter is the local variable defined to receive the values when the corresponding function is invoked.
- Actual parameters are one that occurs when we call a function. Formal parameters occur when we declare or define a function.
- The actual parameter can be passed either by the programmer itself or by the user in real-time. Conversely, the formal parameter is always defined by the programmer.
- We do not define data types along with the actual parameter whereas, we define data types along with the formal parameter.
- The actual parameter can either be a value or a variable. On the other hand, the formal parameter is always a local variable.
- The actual parameters are defined as:
add (a, b);
The formal parameters are defined as:
int add (int x, int y) { //code }
Conclusion
Actual and formal parameters are two different forms of parameters. We pass actual parameters to a calling function whereas, we pass formal parameters while defining or declaring a function. While passing actual parameters we need not mention data types. However, while passing formal parameters we have to mandatorily mention their corresponding data types.
Leave a Reply