Lesson 9 +30 XP

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.

C++
#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:

OUTPUT
inside:  a = 20, b = 10
outside: x = 10, y = 20

2. 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 *.

C++
#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:

OUTPUT
x = 20, y = 10

3. 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.

C++
#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:

OUTPUT
x = 20, y = 10

Call by value copies the arguments; call by reference makes aliasesCall by value copies the arguments; call by reference makes aliases

Comparison

BasisCall by valueCall by addressCall by reference
What is passeda copy of the valuethe address (pointer)an alias (reference)
Can change the caller's variable?noyes, through *yes, directly
Call syntaxf(x)f(&x)f(x)
Parameter syntaxint aint* aint& a
Extra memorya full copyone pointerusually none of its own
Can pass a constant like f(5)?yesnoonly if the parameter is const int&
Available inC 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++.

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:

OUTPUT
Ravi scored 410

4. 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.

C++
#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:

OUTPUT
a = 5, b = -1
a = 105, b = -1

A common real use is giving access to an element of an array or of an object, as vector::operator[] does:

C++
#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:

OUTPUT
70 80 95 60 50
⚠️ Warning

Never 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 like f(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.
🎯 END-TERM EXAM PREP

📝 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) = -1 program 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 const references? (2.5 Marks)