Call By The Reference In C++ - C++ Programming Tutorial
C++ Course / Pointers & References / Call By The Reference In C++

Call By The Reference In C++

BLUF: Mastering Call By The Reference In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Call By The Reference In C++

C++ is renowned for its efficiency. Learn how Call By The Reference In C++ enables low-level control and high-performance computing in the tutorial below.

Whenever a function is declared in a C++ program, there are two methods to invoke the function:

  • Invoking by value
  • Invoking by reference

Before delving into the call-by-reference approach, let's familiarize ourselves with the two methods of invoking a function.

Call by value:

When utilizing the call-by-value approach, a duplicate of the original parameters is transferred to the function when passing arguments or variables. Consequently, any modifications made to the variables within the function do not impact the real values, thereby preventing alteration of the variables.

Call by reference

Passing references to the variables enables changes made to the parameters or variables in the function to directly impact the original variables. This functionality can be implemented in C++ through two methods:

  • Utilizing reference passing
  • Implementing pass-by-reference using pointers
  • Pass by reference in Cpp:

When variables are passed to a function using references in this approach, any modifications made to the variables within the function will directly impact the original values.

Example:

An illustration program showcasing the concept of call by reference in C++:

Example

#include <iostream>
using namespace std;
void swapping(int &var1, int &var2){
    int temp = var1;
    var1 = var2;
    var2 = temp;
}
int main(){
    // declaration and initialization of the two variables
    int variable1 = 1947;
    int variable2 = 2023;
    // printing the two variables before swapping
    cout<< "First variable is " << variable1 << endl;
    cout<< "second variable is " << variable2 << endl;
    // passing the two variables to the swap function
    swapping(variable1, variable2);
    // printing the two variables after swapping
    cout<< "First variable is " << variable1 << endl;
    cout<< "second variable is " << variable2 << endl;
}

Output:

Explanation:

In the aforementioned code snippet, we can observe the presence of two distinct functions: the primary function and the swapping function. Within the "main" function, there exist a pair of variables. Initially, we displayed the values of these two variables prior to their transfer to the swapping function. Subsequently, the variables were passed to the swapping function, resulting in their values being interchanged. Following the function invocation, we exhibited the updated values of the variables, showcasing the successful swapping operation. This scenario serves as a prime illustration of call by reference in the C++ programming language, as modifications applied to the variables within the function are directly reflected in the original variables.

Pass by reference using the pointers:

In this approach, we provide the function with pointers to the variables or objects instead of directly passing the variables or objects themselves. This technique results in the same outcome as the previous method. Any modifications made to the variables within the function will be visible in the original variables.

Example:

A sample code snippet demonstrating call by reference using pointers in C++:

Example

#include<iostream>
using namespace std;
void add(int *a, int *b,int *res){
    *res = *a + *b;
}
void sub(int *a, int *b,int *res){
    *res = *a - *b;
}
void mul(int *a, int *b,int *res){
    *res = *a * *b;
}
int main(){
    int a,b;
    int res = 0;
    int opr =0;
    cout<<"Enter the two operands "<< endl;
    cin >> a >> b ;
    cout<<"Enter the operaton "<< endl;
    cout<<"Enter 1 for addition "<< endl;
    cout<<"Enter 2 for subtraction "<< endl;
    cout<<"Enter 3 for Multiplicatoin "<< endl;
    cin >> opr;
    if(opr == 1){
        add(&a, &b, &res);
        cout<<"The addition is "<< res << endl;
    }else if(opr == 2){
        sub(&a, &b, &res);
        cout<<"The subtraction is "<< res << endl;
    }else if(opr == 3){
        mul(&a, &b, &res);
        cout<<"The multiplication is "<< res << endl;
    }else{
        cout<< "Invalid option "<< endl;
    }
}

Output:

Explanation:

In the provided code snippet, there are four functions: the add method, sub procedure, mul subroutine, and the main function. The add function performs addition on two variables and saves the result in the res variable. The sub procedure subtracts the second variable from the first one and stores the outcome in the res variable, while the mul subroutine multiplies the two variables and saves the product in the res variable.

Here, the res variable along with other variables are passed to the corresponding function via pass-by-reference method using pointers. Within the main function, the variables are declared, user input is obtained for a and b variables, and the res variables are set to zero initially. Consequently, any modifications made to these variables within functions will directly impact the res variable.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience