Javatpoint Logo
Javatpoint Logo

Actual and Formal Parameters in C

In C programming language, parameters are variables that are used to pass values or references between functions. There are two types of parameters: actual and formal parameters. In this article, we will discuss the difference between actual and formal parameters in C, how they work, and their importance in function calls.

Actual Parameters in C:

Actual parameters are the values that are passed to a function when it is called. They are also known as arguments. These values can be constants, variables, expressions, or even other function calls. When a function is called, the actual parameters are evaluated, and their values are copied into the corresponding formal parameters of the called function.

In C, actual parameters are enclosed in parentheses and separated by commas. For example, consider the following function call:

In this function call, 2 and 3 are the actual parameters, and they are passed to the function add, which takes two formal parameters.

Formal Parameters in C:

Formal parameters are the variables declared in the function header that are used to receive the values of the actual parameters passed during function calls. They are also known as function parameters. Formal parameters are used to define the function signature and to specify the type and number of arguments that a function can accept.

In C, formal parameters are declared in the function declaration or definition. For example, consider the following function declaration:

In this function declaration, a and b are the formal parameters. They are used to receive the values of the actual parameters passed during function calls.

Difference between Actual and Formal Parameters:

The main difference between actual and formal parameters is that actual parameters are the values that are passed to a function when it is called, while formal parameters are the variables declared in the function header that are used to receive the values of the actual parameters passed during function calls.

Another difference is that actual parameters can be expressions or other function calls, while formal parameters are always variables. It means that actual parameters can be evaluated at runtime, while formal parameters are defined at compile-time.

Importance of Actual and Formal Parameters:

Actual and formal parameters are important in C programming because they enable the passing of values and references between functions. Functions can be designed to accept parameters that are passed by value, reference, or by pointer. It enables the creation of flexible and reusable code that can be used in different contexts.

Passing parameters by value means that the function receives a copy of the value of the actual parameter. It allows the function to modify the copy without affecting the original value of the actual parameter.

Passing parameters by reference means that the function receives a reference to the memory location of the actual parameter. It allows the function to modify the value of the actual parameter directly.

Passing parameters by pointer means that the function receives a pointer to the memory location of the actual parameter. It allows the function to indirectly modify the actual parameter's value indirectly by dereferencing the pointer.

Passing Parameters by Value:

When passing parameters by value, a copy of the actual parameter's value is made and passed to the function. It means that any modifications made to the parameter inside the function do not affect the original value of the actual parameter. It is useful when we want to manipulate the value of a variable inside a function but don't want to modify the original value outside the function.

Consider the following example:

Output:

a inside function: 6
x outside function: 5

Explanation:

In this example, the function increment takes an integer parameter a by value. The function increments the value of a and prints it to the console. We call the function increment with the variable x as the actual parameter. As we can see, the value of x is not affected by the function increment, even though we passed it as a parameter.

Passing Parameters by Reference:

When passing parameters by reference, the memory address of the actual parameter is passed to the function. It means that any modifications made to the parameter inside the function affect the original value of the actual parameter. It is useful when we want to modify the value of a variable outside the function.

Consider the following example:

Output:

x before swap: 5
y before swap: 10
x after swap: 10
y after swap: 5

Explanation:

In this example, the function swap takes two integer parameters a and b by reference, using pointers. The function swaps the values of the two pointers by dereferencing them and using a temporary variable. We call the function swap with the variables x and y as the actual parameters, using the & operator to get their memory addresses. As we can see, the values of x and y are swapped inside the function swap, and the changes are reflected outside the function.

Passing Parameters by Pointer:

When passing parameters by pointer, a pointer to the actual parameter is passed to the function. It means that any modifications made to the parameter inside the function affect the original value of the actual parameter, just like passing by reference. The difference is that passing by the pointer allows us to modify the pointer itself, not just the value it points to.

Consider the following example:

In this example, the function allocate takes two parameters, p, and size, by pointer and value, respectively. The function allocates an array of integers of size using malloc and sets the pointer p to the beginning of the array. After that, the function initializes the array with values from 0 to size-1. We call the function to allocate the address of the array variable as the actual parameter for p and the value 5 for size. When we run the program, we get an array with values 0 through 4 allocated and initialized in memory.

Actual and Formal Parameters:

The actual parameters are the values or variables that are passed to a function when it is called. On the other hand, the formal parameters are the variables in the function definition that receive the values of the actual parameters. When a function is called, the values of the actual parameters are copied to the formal parameters. It means that any modifications made to the formal parameters inside the function do not affect the values of the actual parameters outside the function.

Consider the following example:

Output:

5 + 10 = 15

Explanation:

In this example, the function add takes two integer parameters a and b by value. The function calculates their sum and prints it to the console. We call the function add with the variables x and y as the actual parameters. As we can see, the values of x and y are not modified by the function add.

Conclusion:

In summary, actual and formal parameters in C programming are used to pass data to functions. Actual parameters can be passed by value, reference, or pointer. Formal parameters are the variables in the function definition that receive the values of the actual parameters. Passing parameters by value makes a copy of the actual parameter's value and passing by reference or pointer passes the memory address of the actual parameter. This means that any modifications made to the parameter inside the function affect the original value of the actual parameter. Understanding actual and formal parameters is essential for writing effective and efficient functions in C programming.


Next TopicBit Stuffing in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA