Call by Value and Call by Reference in C++

User-defined functions are a crucial feature in C++ that allows users to write advanced programs. In C++, YOU can pass arguments to a function either through Call by Value or through Call by Reference. In this tutorial, you learn about call by value and call by reference in C++ and understand these methods in detail.

What Are Parameters?

Parameters refer to the values that are declared inside the parentheses. It passes these values to the function, and a comma separates each parameter from the other parameter. For example, if there is a function for adding or subtracting two numbers, then that function must have two parameters. 

CallByReference_Example1

Here n1, n2 are the parameters that are passed to the function add().

These parameters are of two types.

Formal parameters: These are those parameters that are passed during the declaration/ definition of a function.

Actual parameters: These are those parameters that are passed while calling the function.

Now, you will learn about Call by Value and Call by Reference in C++.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Call by Value Method

Call by Value is a method in which it passes the copies of actual parameters to the formal parameters inside the function. And as you know, it passes the copies, so even if there is any change in the values inside the function, it will not reflect that change in the actual values.

callByReference_Example2

Here copies of the values num1 and num2 are being passed to n1 and n2. The actual parameters are num1, num2, and it passes the copies of the values to the function's formal parameters.

CallByReference_Example3

Call by Reference Method

Call by Reference is a method in which it passes the reference or address of the actual parameter to the function's formal parameters, which means if there is any change in the values inside the function, it reflects that change in the actual values.

callByReference_Example4.

Here &n1 and &n2 are the reference or addresses to num1 and num2, and that is why the change in the formal parameter will be reflected in the actual parameter. 

CallByReference_Example5.

Now let’s have a look at an example.

CallByReference_Example6.

In this example, you assigned the variable salary with the value of 27000. It passes the reference of the variable to the function increment. Inside the function, it increments the value by 5000, and the output at line 27 would be 32000.

Now, as you passed the reference of the value, it will reflect the changes made in the actual value. That means at line 36, the actual value will also get incremented to 32000.

This is the output of the above example, and you can see that the actual value of variable salary has also changed, i.e. salary before the increment.

CallByValue_CallByReference_Example7

Let’s have a look at another example.

CallByValue_CallByReference_Example9.

In this example, the only change is that here you are passing the value through the Call by value method. You passed the copy of the actual parameter to the formal parameter of the function increment. Inside the function, the value is incremented by 5000.

Now, as you know, there is no change in the actual value in the Call by value method. So the value of the variable sal will not change at line 36, i.e. salary before the increment. And only the output at line 27, i.e. salary after increment, will change to 32000.

This is the output of the above example, and you can see there is no change in the actual value, i.e. 27000.

CallByValue_CallByReference_Example10

Prepare Yourself to Answer All Questions!

Automation Testing Masters ProgramExplore Program
Prepare Yourself to Answer All Questions!

Difference Between Call by Value and Call by Reference

Call by Value

Call by Reference

In this method, it passes the value of the variable as an argument.

In this method, it passes the address of the variable as an argument.

In this method, the actual value is not modified.

In this method, the actual value is modified.

This method is slow.

This method is fast.

Both formal and actual parameters have different memory locations.

Both formal and actual arguments are at the same memory location.

That was all about Call by value and call by reference in C++ article.

Conclusion

After reading this tutorial on call by value and call by reference in C++, you would have understood about these two methods and how both of these methods work, along with some examples. You also learned the difference between call by value and call by reference methods.

If you are perhaps looking to build a career in software development, check the Full Stack Developer - MERN Stack by Simplilearn. It can prove to be the ideal solution to help you build your career in the right way.

Do you have any questions regarding this article on Call by value and Call by reference in C++? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about Call by value and Call by reference in C++, click on the following link: Call by value and Call by reference in C++.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.