Signup/Sign In

C++ Call By Reference Program

Hello Everyone!

In this tutorial, we will learn the working of a Call By Reference function call, in the C++ programming language.

Call By Reference Function Call:

In this type of Function Call, the location or the address of the variable is passed instead of the value of the variable itself.

To learn more about this concept, visit https://www.studytonight.com/cpp/call-by-value-and-reference.php, where we have explained the difference between call by value and call by reference function calls.

For better understanding, refer to the well-commented code given below.

Code:

#include <iostream>
#include<vector>
using namespace std;

//Function prototyping as defined after it is being called.  
// It denotes that the method sumOf() takes two parameters which are pointer to an int and returns int
int sumOf(int *, int *);

int main()
{
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of Pass By Reference Function call, in CPP  ===== \n\n";

    //variable declaration
    int num1 = 0, num2 = 0, addition=0;

    cout << "Enther the two numbers you want to add : \n\n";
    cin >> num1;
    cin >> num2;

    /*
    Demonstrating Multi-line Commenting:
        Passing the values stored in the variables num1 and num2 as a parameter to function sumOf().
        The value returned by the function is stored in the variable output
    */

   //It is not always necessary to store the returned value into a variable as it can be directly used as demonstrted below
    cout << "\n\nThe Sum of the two numbers " << num1 << " and " << num2 << ", returned by the function sumOf(), is = " << sumOf(&num1, &num2);

    cout << "\n\n\n";

    return 0;
}


// Defining the function sumOf(a,b) which is called by Passing Values and returns the sum of a and b
int sumOf(int *n1, int *n2)
{
    int sum;
    //Computing the addition of the two values the function is called with
    sum = *n1 + *n2;

    //Returning the addition to the point where this function is called from
    return sum;
}

Output:

C++ Call By Reference

We hope that this post helped you develop a better understanding of the concept of Call by Reference in C++. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.