Course contentsShow
← Computer Science
Lesson 321 of 2,8728. Linked Data StructuresFree lesson

Reference Parameters in Functions

Passing function parameters by reference to allow modification of caller's variables without pointer syntax.

Reference Parameters in Functions

You'll learn how to let functions modify a caller's variable directly—no pointer syntax required.


The Core Idea

You already know that passing a pointer to a function allows modification of the caller's variable. Reference parameters offer a cleaner alternative: you pass a variable "by reference," creating an alias inside the function that directly accesses the original variable—without needing & or * operators everywhere.

Think of it like this: instead of giving someone directions to your house (a pointer), you give them a spare key that opens the exact same door (a reference).

How It Works

When you declare a function parameter with an & symbol attached to the type, it becomes a reference parameter:

void increment(int& num) {  // num is a reference to the caller's variable
    num = num + 1;           // modifies the original directly
}

int main() {
    int value = 5;
    increment(value);  // pass by reference
    // value is now 6
}

Notice:

  • Declaration: int& num creates a reference parameter
  • No pointer syntax: Inside increment, you use num like a normal variable
  • Direct modification: Changes to num affect value in main()

Why Use References Over Pointers?

  1. Cleaner syntax: No dereferencing (*) needed
  2. Safety: References cannot be NULL—they must refer to something valid
  3. Readability: Code looks like regular variable access

Key Takeaway:
Reference parameters create an alias to the caller's variable, allowing direct modification with simple, readable syntax—combining the power of pass-by-reference with the clarity of pass-by-value code.