When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value. Before we discuss function call by value, lets understand the technical word that we will use while explaining this.
- Arguments or Parameters
- Function call by value
- Example 1: Function call by value
- Example 2: Function call by value
- Things to remember
- Learn more about function
Quick links
Arguments or Parameters in C:
Arguments are nothing but variables declared inside the parenthesis () is called arguments (OR Parameters). For example: sum(int a, int b); where variables a and b are the arguments or parameters.
Actual Arguments or Parameters:
The arguments that appear in function calls is called actual arguments (or actual parameters). And these value of an actual parameters is copies into the formal parameters.
Formal Arguments or Parameters:
The arguments that appear in function definition is called formal arguments (or formal parameters).
Remember: we cannot modify the value of the actual parameter by the formal parameter.
For Example: Actual or Formal Arguments
/*Program two find sum of two integers input given by user*/
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
int sum(int, int);
void main()
{
int a, b, sum;
clrscr();
printf("Eneter two numbers: ");
scanf("%d%d\n", &a, &b);
/* Function call by passing value */
sum = sum(a, b);
printf("sum = %d", sum);
getch();
}
/* Creating user defined function OR function definition*/
int sum(int n1, int n2)
{
int result;
result = n1 +n2;
return result;
}
variables n1 and n2 are the formal parameters (or formal arguments). look at the below statements taken form the above example:
/* Creating user defined function OR function definition*/
int sum(int n1, int n2)
Variable a and b are the actual arguments (or actual parameters). look at the below statements taken form the above example:
/* Function call by passing value */
sum(a, b);
The actual parameters can also be the values. Like sum(10, 20), here 10 and 20 are actual parameters.
Now Let's back on the topic.
Function call by value in C:
- When we pass a copy of variables as an actual arguments while calling a function then this is known as function call by value.
- In call by value, the value of the actual parameters is copied into the formal parameters.
- In call by value, we can not modify the value of the actual parameter by the formal parameter.
- In call by value, actual and formal arguments will be created in different memory location.
Example 1: Function call by value
As we know that, in the call by value the actual arguments are copied to the formal arguments, hence any operation performed by function on formal arguments doesn’t affect actual arguments. Lets take an example to understand it better.
#include <stdio.h>
#include <conio.h>
/* Creating user defined function OR function definition*/
int modification(int num1)
{
num1 = num1 + 10;
return num1;
}
void main()
{
int x = 100, y;
clrscr();
/* Function call by passing value */
y = modification(x);
printf(" Value of x = %d \n", x);
printf(" Value of y = %d ", y);
getch();
}
Explanation: We passed the variable x while calling the function, but since we are calling the function using call by value method, only the value of x is copied to the formal parameter num1. Thus change made to the num1 doesn’t reflect in the x.
Output
Value of x = 100
Value of y = 110
Example 2: Swapping numbers using function call by value
#include <stdio.h>
void swap(int , int); //prototype of the function
void main()
{
int num1 = 10;
int num2 = 11;
printf("Value before swapping: %d, %d\n", num1, num2);
swap(num1, num2); // num1 and num2 are the actual arguments or parameters
printf("Value after swapping: %d, %d\n", num1, num2);
}
void swap (int a, int b) // a and b are the formal arguments or parameters
{
int t;
t = a;
a = b;
b = t;
}
Output
Value before swapping: 10, 11
Value after swapping: 11, 10
Things to remember:
- In call by value, a copy of the variable is passed whereas in call by reference, a variable itself is passed.
- In call by value, actual and formal arguments will be created in different memory location whereas in call by reference, actual and formal arguments will be created in the same location
- In the call by value, the actual arguments are copied to the formal parameters. Hence any operation performed by function on formal arguments doesn’t affect actual arguments.
- The arguments that appear in function calls is called actual arguments (or actual parameters). And the arguments that appear in function definition is called formal arguments (or formal parameters).
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.