Loading lessons...
Call by Value, Call by Reference & Return by Reference
Call by Value, Call by Reference & Return by Reference
How arguments are passed decides whether a function can change the caller's variables. C has only call by value (and simulates the rest with pointers). C++ adds reference variables, which give a clean call by reference and allow a function to return a reference, so a function call can even appear on the left of an assignment.
1. Call by Value
The function receives a copy of each argument. Changes to the copy do not affect the original.
#include <iostream>
using namespace std;
void swapByValue(int a, int b) { // a and b are copies
int t = a;
a = b;
b = t;
cout << "inside: a = " << a << ", b = " << b << endl;
}
int main() {
int x = 10, y = 20;
swapByValue(x, y);
cout << "outside: x = " << x << ", y = " << y << endl; // unchanged
return 0;
}Output:
inside: a = 20, b = 10
outside: x = 10, y = 202. Call by Address (the C way)
The addresses of the arguments are passed, and the function works through pointers. This changes the originals, but the code is cluttered with & and *.
#include <iostream>
using namespace std;
void swapByAddress(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
int main() {
int x = 10, y = 20;
swapByAddress(&x, &y); // pass addresses
cout << "x = " << x << ", y = " << y << endl;
return 0;
}Output:
x = 20, y = 103. Call by Reference (the C++ way)
The parameters are reference variables: aliases for the actual arguments. The function works directly on the caller's variables, but the code looks like call by value. No & at the call, no * in the body.
#include <iostream>
using namespace std;
void swapByReference(int& a, int& b) { // a and b are aliases for x and y
int t = a;
a = b;
b = t;
}
int main() {
int x = 10, y = 20;
swapByReference(x, y); // looks like call by value
cout << "x = " << x << ", y = " << y << endl;
return 0;
}Output:
x = 20, y = 10Call by value copies the arguments; call by reference makes aliases
Comparison
| Basis | Call by value | Call by address | Call by reference |
|---|---|---|---|
| What is passed | a copy of the value | the address (pointer) | an alias (reference) |
| Can change the caller's variable? | no | yes, through * | yes, directly |
| Call syntax | f(x) | f(&x) | f(x) |
| Parameter syntax | int a | int* a | int& a |
| Extra memory | a full copy | one pointer | usually none of its own |
Can pass a constant like f(5)? | yes | no | only if the parameter is const int& |
| Available in | C and C++ | C and C++ | C++ only |
Passing large objects: const references
Copying a big object for call by value is slow. Passing it by reference avoids the copy, and adding const stops the function from changing it by mistake. This is the standard way to pass objects in C++.
#include <iostream>
using namespace std;
struct Student {
char name[40];
int marks[5];
};
// const reference: no copy is made, and the function cannot modify s
int total(const Student& s) {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum = sum + s.marks[i];
}
// s.marks[0] = 100; // error: s is read-only
return sum;
}
int main() {
Student st = {"Ravi", {78, 85, 69, 90, 88}};
cout << st.name << " scored " << total(st) << endl;
return 0;
}Output:
Ravi scored 4104. Return by Reference
A function can also return a reference. The call then stands for the actual variable, not a copy of its value, so it can be used on the left side of an assignment.
#include <iostream>
using namespace std;
int& larger(int& x, int& y) { // returns a reference to the larger variable
if (x > y) {
return x;
} else {
return y;
}
}
int main() {
int a = 5, b = 9;
larger(a, b) = -1; // assigns -1 to b, the larger one
cout << "a = " << a << ", b = " << b << endl;
larger(a, b) += 100; // now a (5) is larger, so a becomes 105
cout << "a = " << a << ", b = " << b << endl;
return 0;
}Output:
a = 5, b = -1
a = 105, b = -1A common real use is giving access to an element of an array or of an object, as vector::operator[] does:
#include <iostream>
using namespace std;
int marks[5] = {70, 80, 90, 60, 50};
int& element(int i) { // returns a reference to marks[i]
return marks[i];
}
int main() {
element(2) = 95; // change marks[2] through the function
for (int i = 0; i < 5; i++) {
cout << element(i) << " ";
}
cout << endl;
return 0;
}Output:
70 80 95 60 50Never return a reference to a local variable. A local variable is destroyed when the function returns, so the reference would refer to memory that no longer exists (a dangling reference): int& bad() { int x = 5; return x; } compiles with a warning but its behaviour is undefined. Return references only to variables that outlive the call: reference parameters, global or static variables, or members of an object.
Key points to remember
- Call by value: a copy; the original is safe but cannot be changed.
- Call by reference:
void f(int& a); the parameter is an alias, the call looks likef(x).- Use
const Type&to pass large objects without copying and without allowing changes.- Return by reference:
int& f(...); the call can appear on the left of=.- Never return a reference (or pointer) to a local variable.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain call by value and call by reference with a swap program. (5 Marks)
- Write: both programs with output, the diagram, and the comparison table.
- Q2. What is return by reference? Explain with an example where a function call appears on the left of an assignment. (5 Marks)
- Write: the
larger(a, b) = -1program and the warning about local variables. - Q3. Differentiate between call by address and call by reference. (2.5 Marks)
- Q4. Why are objects usually passed as
constreferences? (2.5 Marks)