What is the difference between passing arguments by reference and passing an argument by value explain with C++ example?

In C++ we can pass arguments into a function in different ways. These different ways are −

  • Call by Value
  • Call by Reference
  • Call by Address

Sometimes the call by address is referred to as call by reference, but they are different in C++. In call by address, we use pointer variables to send the exact memory address, but in call by reference we pass the reference variable [alias of that variable]. This feature is not present in C, there we have to pass the pointer to get that effect. In this section we will see what are the advantages of call by reference over call by value, and where to use them

Call by Value

In call by value, the actual value that is passed as argument is not changed after performing some operation on it. When call by value is used, it creates a copy of that variable into the stack section in memory. When the value is changed, it changes the value of that copy, the actual value remains the same.

Example Code

 Live Demo

#include using namespace std; void my_function[int x] {    x = 50;    cout

Chủ Đề