Formal Parameters in C
In C Programming language, Parameters are defined as the variables used to convey values or references between functions. Parameters are also known as arguments. Parameters are the inputs passed in a function. Actual and Formal Parameters are the two different types of parameters.
Parameters are passed in a function in different ways:
- At the time of the declaration
- At the time of definition
- At the time of calling the function.
Formal Parameters in the function
Formal Parameters are the type of arguments or parameters that are passed in the function at the time of defining or declaring the function. The Formal Parameters accept the values of actual parameters which are specified at the timing of the definition of the function in the same order as they are given by the calling function. These values are accessed by the statements in the function definition by using the variable name specified by the formal parameters.
Formal Parameters are declared within the header of a function and are enclosed by parenthesis. The formal parameters in a function contain variable names and their data types. Every argument or parameter which is passed in a function is separated by a comma.
At the time of calling a function, the values of actual parameters are copied to the formal parameters. In other words, any changes made to the formal parameters defined inside the function header do not make any effect on the actual parameters outside the function block.
Syntax of Formal Parameters
function_return_type function_name (parameter_return_type, parameter_name)
{
//statements
}
Example of Formal Parameter in C
int sum_n(int n1, int n2)
{
return n1 + n2;
}
In this code, the variables n1 and n2 are formal parameters. In the calling function, the actual parameters copy the Formal Parameter and will give the outputs accordingly.
Let’s explore more about the formal parameters in C.
Program 1: Illustration of the formal parameters using a function in
CODE
#include<conio.h>
#include <stdio.h>
int sum_n(int n1, int n2)
{
int s;
s = n1 + n2;
return s;
}
int main()
{
int a = 5, b = 8;
int ans=0;
ans = sum_n(a, b);
printf("The sum is : %d", ans);
return 0;
}
OUTPUT
![Formal Parameters in C](https://static.tutorialandexample.com/c/formal-parameters-in-c1.png)
In this code, a function named sum_n is defined which returns the sum of the variables. In this function, two formal parameters n1 and n2 are defined. In the main function, two variables x and y, which are actual parameters, are declared which copies the formal parameters at the timing of calling and prints the value of the sum.
There are different ways of passing formal parameters in a function. They are:
- By Value
- By Reference
Parameters Passed by Value: It is a method by which a copy of the actual variables is sent to the function which gives the result. In simple words, any changes made to the formal parameters in the function do not affect the value of the actual parameters.
Example 2: Program to illustrate the concept of passing the parameters by the value in C.
CODE
#include<conio.h>
#include <stdio.h>
void inc(int x)
{
x++;
printf("The Value of x inside the function is: %d\n", x);
}
int main()
{
int x = 15 ;
inc(x);
printf("The value of x outside the function is: %d\n",x);
return 0;
}
OUTPUT
![Formal Parameters in C](https://static.tutorialandexample.com/c/formal-parameters-in-c2.png)
In this code, the actual parameters are called through formal parameters by value. As we have printed both the value of formal parameter x inside the function and the value of actual parameter x outside the function, we have increased the value of x inside the inc function but the value of actual parameter x outside the function does not change. (The actual and formal parameters variable names can be the same).
Parameters Passed by Reference: It is a method by which the address of the value is passed through the called function instead of passing the direct value. It means making any change in the formal parameter will also reflect in the actual parameter.
Example: Program to illustrate the concept of passing the parameters by reference in C.
CODE
#include <stdio.h>
void swap_func (int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
int main()
{
int a = 20, b = 40 ;
printf(" Before swapping: \n a = %d\n b = %d\n", a, b);
swap_func (&a, &b); // call by reference
printf(" After swapping: \n a = %d\n b = %d\n", a, b);
return 0;
}
OUTPUT
In this code, we have called the value by reference, the values before calling are printed and the values after calling the function are also printed. The value by references is called by adding a ‘&’ mark which indicates that it is referring to the address of the variable.
Difference between Formal and Actual Parameters
Formal Parameters | Actual Parameters |
1. The formal parameters can be used in place of actual parameters if no value is passed in the actual parameter. | 1. These are the values that are passed in the function at the time of the function call. |
2. These are the values that will be passed into the function. | 2. These are the input variables of the function. |
3. Formal parameters can only be used in the function definition. | 3. Actual Parameters can only be used in the function call. |
4. The Formal Parameters are used by the function for the usage in function call through the actual parameter. | 4. The actual parameters are used for evaluation before passing into the function. |
Advantages of Formal Parameters in C
- It determines the data types of the variables.
- It prevents any errors while calling the function.
- It is used to define the variables that get the values after calling the function.
Disadvantages of Formal Parameters in C
- It can make the function call more complex.
- We cannot pass huge data structures as formal parameters.
- Debugging can be difficult if wrong parameters are added.