Passing by Value and Reference in a function.

 Passing by Value and Reference in a function.

·       Passing arguments to a function in two primary ways: pass by value and pass by reference.

 

1. Pass by Value

  • Default Mechanism.
  • When a parameter is passed by value, a copy of the argument’s value is made and passed to the function.
  • The function works with this copy, and any modifications to it do not affect the original variable.

 

Syntax

void function_name(data_type parameter) {

    // Modify parameter

}

 

Example

#include <iostream>

using namespace std;

 

void increment(int x) {

    x++; // Modifies the copy, not the original

    cout << "Inside function: " << x << endl;

}

 

int main() {

    int num = 10;

    increment(num);

    cout << "Outside function: " << num << endl;

    return 0;

}

 

Output

Inside function: 11

Outside function: 10

 

Pros

  • Safety: The original data is protected from modification.
  • Simplicity: No unintended side effects.

 

Cons

  • Memory Overhead: Copying large objects (e.g., structs, arrays) can be inefficient.
  • No Direct Modification: You can’t change the original variable.

 

2. Pass by Reference

  • Function receives a reference to the original variable (its memory address), not a copy.
  • Changes made to the parameter inside the function directly affect the original variable.

 

Syntax

void function_name(data_type &parameter) {

    // Modify parameter (affects original)

}

 

Example

#include <iostream>

using namespace std;

 

void increment(int &x) {

    x++; // Modifies the original variable

    cout << "Inside function: " << x << endl;

}

 

int main() {

    int num = 10;

    increment(num);

    cout << "Outside function: " << num << endl;

    return 0;

}

 

Output

Inside function: 11

Outside function: 11

 

Pros

  • Efficiency: No copying of data, especially useful for large objects like structs or classes.
  • Direct Modification: Allows the function to alter the original variable.

 

Cons

  • Risk: Unintended changes to the original data can occur.
  • Clarity: You must be explicit about modifying behaviour to avoid confusion.

 

Pass by Pointer (Similar to Reference)

  • Involves passing the memory address of a variable explicitly using pointers (*).
  • Modify the original data via the pointer, but must dereference it manually.
  • Requires explicitly passing the address (&num) and using dereference (*x).
  • Allows handling nullptr safely.

 

Example:

#include <iostream>

using namespace std;

 

void increment(int *x) {

    (*x)++; // Dereference pointer to modify original

}

 

int main() {

    int num = 10;

    increment(&num); // Pass address of num

    cout << num << endl; // Outputs 11

    return 0;

}

 

Pointer vs. Reference

  • Reference: Cleaner syntax, automatically dereferenced, cannot be null.
  • Pointer: Explicit address manipulation, can be null, requires * to access the value.

 


Key Differences

 

Feature

Pass by Value

Pass by Reference

Data Handling

Copy of the argument

Reference to the argument

Original data Modified

No

Yes

Syntax

type param

type &param

Memory Usage

Creates a copy (more memory)

No copy (less memory)

Use Case

Protect original data

Modify original data



 

Post a Comment

0 Comments