Actual Parameter and Formal parameter
Actual Parameters: The parameter passed to a function at the time of call.
Formal Parameters: The parameter received by a function to call this function.
What is Call by Value?
In call by value method, the value of the actual parameters is copied into the formal parameters.
OR
We can say that the value given by the user to a variable is used by function when a user call that function.
In call by value method, we can not modify the value of the actual parameter by the formal parameter
In call by value, actual and formal parameters have different memory location besides actual parameters copies the value to the formal parameters.
The actual parameter is the argument which is used in the function call whereas formal parameter is the argument which is used in the function definition.
Example Call by Value
// C program to illustrate
// call by value
#include <stdio.h>
#include<conio.h>
// Function Prototype
void swapx(int x, int y);
// Main function
void main()
{
int a = 10, b = 20;
// Actual Value
printf(“a=%d b=%d\n”,a,b);
// Pass by Values
swapx(a, b);
printf("a=%d b=%d\n", a, b);
}
// Swap functions that swaps
// two values
void swapx(int x, int y)
{
int t;
t = x;
x = y;
y = t;
printf("x=%d y=%d\n", x, y);
}
Output Call by Value:
a=10 //actual value before swap
b=20 //actual value before swap
x=20 // after swap
y=10 // after swap
a=10 //actual value after swap
b=20 //actual value after swap
What is Call by Reference?
In call by reference, the address of the variable is passed into the function call as the actual parameter.
OR
We can say that the address of the variable given by the user to a variable is used by function when a user call that function.
In call by value method, we can modify the value of the actual parameter by the formal parameter.
In call by value, actual and formal parameters have same memory space because the address of the actual parameters used by the formal parameters.
The address of the argument of actual parameter is used in the function call whereas formal parameter is used the reference of the argument in the function definition.
Example Call by Reference
// C program to illustrate
// call by value
#include <stdio.h>
#include<conio.h>
// Function Prototype
void swapx(*int x, *int y);
// Main function
void main()
{
int a = 10, b = 20;
// Actual Value
printf(“a=%d b=%d\n”,a,b);
// Pass by Values
swapx(&a, &b);
printf("a=%d b=%d\n", a, b);
}
// Swap functions that swaps
// two values
void swapx(int x, int y)
{
int t;
t = *x;
*x = *y;
*y = t;
printf("x=%d y=%d\n", *x, *y);
}
Output Call By Reference
a=10 //actual value before swap
b=20 //actual value before swap
x=20 // after swap
y=10 // after swap
a=20 //actual value after swap
b=10 //actual value after swap
Output Comparison
Call
by Value
|
Call
by Reference
|
Actual values of a and b remain
unchanged even after swapping the values of x and y.
|
Actual values of a and b get
changed after swapping values of x and y.
|
In call by values we cannot alter
the values of actual variables through function calls.
|
In call by reference we can alter
the values of variables through function calls.
|
Values of variables are passes by
Simple technique.
|
Pointer variables are necessary to
define to store the address values of variables.
|
Watch the Video Lecturer Here--