References and Functions in C++

 References and Functions


Reference

A reference is an alias (another name) for an existing variable.
Once a reference is created, it cannot be changed to refer to another variable.

 

Definition syntax:

datatype &reference_name = variable_name;

 

Example:

int a = 10;

int &ref = a;   // ref is a reference to a

 

Both a and ref refer to the same memory location.

ref = 20;   // changes a also

cout << a;  // Output: 20


Key Points about References

Concept

Description

Declaration

Must be initialized at the time of declaration

Cannot be NULL

Unlike pointers

No re-assignment

Once assigned, cannot refer to another variable

Access

Used just like the original variable

Syntax simplicity

No need for * or & like in pointers


 

Difference between Pointer and Reference

Feature

Pointer

Reference

Declaration

int *p;

int &r = x;

Null value

Can be NULL

Cannot be NULL

Reassignment

Can point to another variable

Cannot change reference

Dereference operator

*p

Not needed

Memory

Has its own memory address

Shares the same memory as a variable


 

Reference as Function Parameters

References are often used to pass arguments to functions efficiently.

There are three types of function arguments in C++:

  1. Call by Value
  2. Call by Reference
  3. Call by Address

 (a) Call by Value

  • A copy of the argument is passed to the function.
  • Changes inside a function do not affect the original variable.

 

Example:

#include <iostream>

using namespace std;

 

void change(int x) {

    x = 20;

}

 

int main() {

    int a = 10;

    change(a);

    cout << a;  // Output: 10 (not changed)

}


 

(b) Call by Reference

  • The original variable is passed using a reference.
  • Any change inside the function affects the original variable.

Example:

#include <iostream>

using namespace std;

 

void change(int &x) {  // reference parameter

    x = 20;

}

 

int main() {

    int a = 10;

    change(a);

    cout << a;  // Output: 20 (changed)

}


Usability of reference

  • Saves memory (no copy made)
  • Allows direct modification of arguments
  • Faster execution

 (c) Call by Address (using pointers)

Just for comparison:

#include <iostream>

using namespace std;

 

void change(int *x) {

    *x = 30;

}

 

int main() {

    int a = 10;

    change(&a);

    cout << a;  // Output: 30

}


Reference as Function Return

A function can also return a reference to a variable.
Useful when you want the function to behave like an alias to an existing variable.

Example:

#include <iostream>

using namespace std;

 

int& getValue(int &x) {

    return x;

}

 

int main() {

    int a = 10;

    getValue(a) = 50;   // modifies a directly

    cout << a;  // Output: 50

}

Note:- 
Don’t return a reference to a local variable (because it gets destroyed when function ends).



Const Reference

If you want to pass large data without copying but don’t want to modify it,
use a const reference.

Example:

#include <iostream>

using namespace std;

 

void print(const string &name) {

    cout << "Hello, " << name;

}

 

int main() {

    string s = "Pankaj";

    print(s);

}

Benefits:

  • No copy (faster)
  • Cannot be changed inside the function (safe)

Use References in Functions

Use Case

Type of Reference

To modify the actual argument

Non-const reference

To improve efficiency without modification

Const reference

For small built-in data types

Pass by value (simple)



Example Program — Swapping using Reference

#include <iostream>

using namespace std;

 

void swapNums(int &x, int &y) {

    int temp = x;

    x = y;

    y = temp;

}

 

int main() {

    int a = 10, b = 20;

    swapNums(a, b);

    cout << "a = " << a << ", b = " << b;

    return 0;

}

Output:

a = 20, b = 10


Summary

Concept

Description

Reference

Alternate name for a variable

Call by reference

Pass the actual variable, changes reflect outside

Const reference

Prevents modification, improves efficiency

Return by reference

Returns the alias of a variable (careful with locals)

Benefit

Saves time, memory, and avoids unnecessary copies



 

Post a Comment

0 Comments