Call by Value, Address & Reference in C++: Swapping & Return by Reference
1. Actual vs. Formal Parameters (In Simple Words)
When you work with functions in C++, there are two types of parameters:
- Actual Parameters (Arguments): The real values or variables you pass when calling the function from
main()(for example, inswap(a, b);,aandbare actual parameters). - Formal Parameters: The parameter variables written in the function definition header that receive those values (for example, in
void swap(int x, int y),xandyare formal parameters).
2. The Three Ways to Pass Parameters in C++
main().&a) is passed to a pointer (int *x). Changes made using *x modify the original variable.int &x) is passed. Directly modifies the original variable with clean, simple syntax without pointers!3. The Classic Swapping Problem: Comparing All Three
Let us understand the three methods by trying to swap two numbers (a = 10 and b = 20):
A. Call by Value (Does NOT Swap Original Values)
Analogy: Think of printing a photocopy of your notes and giving it to a friend. If your friend writes on the photocopy, your original notes stay 100% clean and unchanged!
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
// x and y are swapped inside this function only!
}
int main() {
int a = 10, b = 20;
swapByValue(a, b);
// In main(): a is still 10, b is still 20! Swapping FAILED.
return 0;
}
Why it fails: x and y are just temporary copies. When the function ends, x and y are erased from memory, leaving a and b unchanged.
B. Call by Address (Swaps Using C Pointers)
Here, instead of values, we pass the memory addresses (&a, &b) to pointers (*x, *y).
void swapByAddress(int *x, int *y) {
int temp = *x; // Read value at address x
*x = *y; // Put value at address y into address x
*y = temp; // Put temp into address y
}
int main() {
int a = 10, b = 20;
swapByAddress(&a, &b); // Pass memory addresses using &
// Now a = 20 and b = 10! Swapping SUCCEEDED.
return 0;
}
Downside: You have to write & when calling the function and * everywhere inside the function, which can get confusing and messy.
C. Call by Reference (The Best C++ Way)
In C++, you can pass parameters as references (&x). A reference is simply an alias (nickname) for the original variable.
void swapByReference(int &x, int &y) { // x and y are nicknames for a and b
int temp = x;
x = y;
y = temp;
}
int main() {
int a = 10, b = 20;
swapByReference(a, b); // Clean and simple call! No & or * needed here!
// Now a = 20 and b = 10! Swapping SUCCEEDED.
return 0;
}
Why this is the best: You get all the power of modifying original variables without the messy pointer syntax!
4. Comprehensive Comparison Table
This table is frequently asked in DAVV BCA exams:
| Feature | Call by Value | Call by Address (Pointers) | Call by Reference (C++) |
|---|---|---|---|
| What is passed? | A copy of the value | The memory address (&a) |
A reference alias (int &x) |
| Original Value Changes? | No (stays unchanged) | Yes (modifies original) | Yes (modifies original) |
| Call Syntax | swap(a, b); |
swap(&a, &b); |
swap(a, b); |
| Function Header | void swap(int x, int y) |
void swap(int *x, int *y) |
void swap(int &x, int &y) |
| Extra Memory Used? | Creates new copy variables | Takes 4/8 bytes for pointers | Zero extra memory |
| Can it be NULL? | No | Yes (Pointers can be NULL) |
No (References cannot be NULL) |
5. Return by Reference in C++
In regular C, a function can only return a value. But in C++, a function can also return a reference to a variable!
When a function returns a reference, you can actually put the function call on the LEFT-HAND SIDE (LHS) of an assignment statement (=):
#include <iostream>
using namespace std;
int x = 10;
int y = 20;
// Returns a reference to whichever global variable is larger
int& getLarger() {
if (x > y)
return x;
else
return y;
}
int main() {
cout << "Before: x = " << x << ", y = " << y << endl;
// FUNCTION CALL ON THE LEFT SIDE OF '=' !
getLarger() = 99; // Since y (20) > x (10), 99 is assigned directly to y!
cout << "After: x = " << x << ", y = " << y << endl;
// Output: After: x = 10, y = 99
return 0;
}
Important Rule for Exams:
Never return a reference to a local variable! Local variables get destroyed as soon as the function finishes. If you return a reference to a destroyed variable, your program will get garbage data or crash. Only return references to global or static variables!
DAVV Exam Preparation Tips
Frequently Asked 5-Mark Questions:
- Differentiate between Call by Value and Call by Reference with code snippets.
Model Answer:- Call by Value passes a duplicate copy; original data cannot be modified.
- Call by Reference passes an alias (
int &x); changes affect the original variable directly. - Write the small swapping snippet for both.
- What is Return by Reference in C++? Give a short example.
Model Answer:- Explain that returning a reference allows a function call to appear on the left-hand side of
=. - Provide the 10-line
int& getLarger()example. - Mention why local variables must never be returned by reference.
- Explain that returning a reference allows a function call to appear on the left-hand side of
Frequently Asked 10-Mark Questions:
- Explain the three parameter passing mechanisms in C++: Call by Value, Call by Address, and Call by Reference. Write complete programs to swap two numbers using each technique and compare them.
Model Answer:- Explain Actual vs. Formal parameters.
- Write full code for:
swap(int a, int b)showing why it fails.swap(int *a, int *b)showing pointer dereferencing.swap(int &a, int &b)showing reference passing.
- Draw the full comparison table covering syntax, memory overhead, and pointer vs reference behavior.