Definition: In call by value, a copy of the actual argument's value is passed to the function. The function works on this separate copy.
Definition: In call by value, a copy of the actual argument's value is passed to the function. The function works on this separate copy.
x = 10 is passed to a function, the function receives
a copy of the value 10. Changing the parameter inside
the function does not change the original x.
The value of the actual argument is copied into the formal parameter. Both variables therefore hold separate values.
#include <stdio.h>
void change(int a)
{
a = 20; // change only the local copy
}
int main()
{
int x = 10;
change(x); // pass value of x
printf("x = %d
", x);
return 0;
}
x = 10
a, not the
original variable x.
In call by value, the formal parameter gets a separate copy of the value supplied by the caller.
Before function call:
x = 10
After calling change(x):
Original variable Function parameter
x = 10 a = 10
↓ copy of value ↓
Changing a to 20:
x = 10 a = 20
Imagine giving someone a photocopy of a document. They can write on the copy, but the original document remains unchanged.
Write a C program to demonstrate that swapping two values inside a function using call by value does not change the original values in the calling function.
Pass two integer variables to a function and swap the received parameters using a temporary variable.
In call by value, the function receives copies of the arguments. Therefore, swapping the formal parameters changes only those copies and the original variables remain unchanged.
#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp = a; // store a temporarily
a = b;
b = temp; // swap the copied values
}
int main()
{
int x = 10, y = 20;
printf("Before function call: x = %d, y = %d
", x, y);
swap(x, y); // pass values of x and y
printf("After function call: x = %d, y = %d
", x, y);
return 0;
}
Before function call: x = 10, y = 20 After function call: x = 10, y = 20
The swap occurs only inside the function because a and
b are copies. The original x and
y remain unchanged.
| Inside Function | Calling Function |
|---|---|
| Works with formal parameter. | Contains original variable. |
| Parameter contains a copy of the value. | Original value remains unchanged by parameter assignment. |
Example: a = 20 |
Original x can remain 10. |
Definition: Call by value passes a value to a function, while the commonly used "call by reference" technique in C passes an address using a pointer so the function can work with the caller's original variable.
change(x)change(&x)
| Concept | Remember |
|---|---|
| What is passed? | Copy of the value. |
| Parameter | Separate from original variable. |
| Original variable | Not changed by parameter assignment. |
| Typical example | Swap using ordinary parameters does not swap originals. |
Watch a beginner-friendly explanation of call by value in C.
A short handwritten-style revision sheet for call by value will be provided here.
Use the mind map for quick revision of value copying, formal parameters and original variables.