Function Argumants - Call By Value
Call by value
"Call by value" is a method of passing arguments to a function in C in which the function receives a copy of the values of the arguments, rather than a reference to the original arguments.
This means that any changes made to the arguments within the function will not affect the values of the original arguments outside the function.
When a function is called with "call by value", the values of the arguments are passed to the function, and the function receives a separate copy of these values.
This allows the function to operate on the values of the arguments without modifying the original arguments.
Call by value - Example
Here's an example of call by value in C:
#include <stdio.h>
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
int a = 3, b = 5;
printf("Before swapping: a = %d, b = %d\n", a,
b); // Print the values of a and b before the swap
swap(a, b); // Call the swap function with call by value
printf("After swapping: a = %d, b = %d\n", a,
b); // Print the values of a and b after the swap
return 0;
}
Output:
Explanation
The
swapfunction takes two integer argumentsxandy, and swaps their values.When the function is called from the
mainfunction with call by value, the values ofaandbare passed to the function and the function receives separate copies of these values.Within the
swapfunction, the values ofxandyare swapped, but the values ofaandbin themainfunction remain unchanged.
Call by value - Another Example
Here's another example of call by value in C:
#include <stdio.h>
int square(int x)
{
return x * x;
}
int main()
{
int num = 5;
printf("Before calling the function: num = %d\n",
num); // Print the value of num before the function call
int result = square(num); // Call the square function with call by value
printf("After calling the function: num = %d\n",
num); // Print the value of num after the function call
printf("Result of the function: %d\n",
result); // Print the result of the function
return 0;
}
Output:
Explanation
The
squarefunction takes an integer argumentxand returns its square.When the function is called from the
mainfunction with call by value, the value ofnumis passed to the function, and the function receives a separate copy of this value.Within the
squarefunction, the value ofxis squared and the result is returned.However, the value of
numin themainfunction remains unchanged.
When to use Call by value
When the caller does not want the function to modify the original values: This allows the caller to use the original values for other purposes after the function call, without being affected by any changes made by the function.
When the function does not need to modify the values of its arguments, but only needs to perform some calculation or processing based on the values.
For example: When finding the square of a number, it is not necessary to modify the original number, and call by value can be used to pass the number to the function.
Appropriate for passing values of simple data types, such as integers, floating-point numbers, and characters. In such cases, creating a copy of the values and passing them to the function is relatively efficient and does not result in any significant overhead.
Please note, in situations where the function needs to modify the values of its arguments, "call by value" may not be appropriate, and other methods of argument passing, such as "call by reference" or "call by pointer", should be used.