C++

Passing Arguments by Reference in C++ Function

A function can be defined in the code with argument or without argument. The value of the argument can be passed by value or reference into the argument of the function. When the argument value of the function is passed by the reference then both the argument variable and the variable that passes the value by reference to that argument, share the same location in the memory.

So, the value of the variable will be changed if the value of the argument variable is changed inside the function. The ‘&’ symbol is used in the argument of the function to declare the argument by reference. The different ways to pass the argument by reference in C++ function have shown in this tutorial.

Example-1: Pass a single argument by reference

The way to pass a single argument by reference to a function has shown in the following example. Here, a string value is initialized into a variable named strVal. This variable has passed as the argument of the function named FunctionByReference().

The argument by reference variable named str has been used in the function argument. The value of the reference variable will be changed after calling this function. The value of strVal is printed before and after calling the function to check how the reference variable works.

//Include necessary library

#include <iostream>

using namespace std;

//Declare the function with the call by reference argument

void FunctionByReference(string& str)

{

        //Modify the reference value

        str = "I like C++ programming";

}

int main()

{

        //Initialize a string variable

        string strVal = "I like programming";

        //Print the initial value of the variable

        cout << "The value of strVal before calling the function:" << strVal << '\n';

        //Call function with the string variable

        FunctionByReference(strVal);

        //Print the modified value of the variable

        cout << "The value of strVal after calling the function is:" << strVal << '\n';

        return 0;

}

Output:

The following output will appear after executing the above code. The value of the strVal was ‘I like programming’ before calling the function and the value of this variable is changed to ‘I like C++ programming’ after calling the function for passing the argument by reference.

Example-2: Modify the content of reference variable based on condition

The following example shows the way to change the value of the reference variable based on the specific condition. Here, the function named CalculateGrade() will take the first argument by reference and the second argument by value.

The value of the first argument will be changed based on the value passed by the second argument. Two values will be taken from the user and passed to the function and the modified value of the variable passed by reference will be printed after executing the code.

///Include necessary library

#include <iostream>

using namespace std;

/*

Declare the function with a call by reference argument

and a call by value argument to calculate the grade

and modify the reference variable

*/


void CalculateGrade(string& info, int marks)

{

        if(marks >= 80)

                info = info + " got A+.";

        else if(marks <80 && marks>=75)

                info = info + " got A.";

        else if(marks <75 && marks>=70)

                info = info + " got A-.";

        else

                info = "No grading information found.";

}

int main()

{

        string info;

        int marks;

        //Take inputs from the user

        cout << "Enter the student ID:" ;

        cin >> info;

        cout << "Enter the obtained marks:" ;

        cin >> marks;

        //Call function with two variables

        CalculateGrade(info, marks);

        //Print the modified value of reference variable

        cout << info << '\n';

        return 0;

}

Output:

The following output will appear after executing the above code. The output shows that the value of the reference variable is changed from ‘784534’ to ‘784534 got A+’ for the value of mark 84.

Example-3: Passing multiple arguments by reference

The following example shows the way to pass multiple arguments by values into a function and change the values of these arguments based on the values of the other arguments. Three inputs will be taken from the user before calling the function.

Here, bonus(%) and total variables will be passed as arguments by reference, and two other variables, salary, and experience will be passed by value into the function named CalculateSalaryWithBonus when it will be called. The function will modify the values of bonus and total variables based on other variables.

//Include necessary libraries

#include <iostream>

#include <string>


using namespace std;

//Declare the function with the call by reference argument

void CalculateSalaryWithBonus(int sal, int yr, float& bonus, double& total)

{

        //Set the bonus based on the experience

        if(yr >= 5)

                bonus = 0.10;

        else if(yr >= 2)

                bonus = 0.07;

        total = sal + sal*bonus;

}

int main()

{

        //Declare necessary variables

        float bonus = 0.05;

        double total=0.0;

        int salary, experience;

        string name;

        //Take input from the user

        cout << "Enter the employee name:" ;

        getline(cin , name);

        cout << "Enter the salary:" ;

        cin >> salary;

        cout << "Enter the years of experience:" ;

        cin >> experience;

        //Call the function to calculate the salary based on the bonus

        CalculateSalaryWithBonus(salary, experience, bonus, total);

        //Print the salary details

        cout << "Salary information with bonus: \n";

        cout << "Name: " << name << "\n";

        cout << "Salary: " << salary << "\n";

        cout << "Experience: " << experience << "years" << "\n";

        cout << "Bonus(%): " << bonus << "\n";

        cout << "Salary with bonus: " << total << "\n";

        return 0;

}

Output:

The following output will appear after executing the above code based on the input taken from the user.  According to the output, 10 has taken as the experience value that is greater than 5. So, the value of the bonus has been modified to 0.10 and the value of the total has been counted based on the bonus.

Conclusion

The use of passing the argument by reference into the C ++ function has been explained in this tutorial by using various examples for helping the new C++ coders to use this feature in their code properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.