
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Differences between pass by value and pass by reference in C++
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
#include<iostream> using namespace std; void my_function(int x) { x = 50; cout << "Value of x from my_function: " << x << endl; } main() { int x = 10; my_function(x); cout << "Value of x from main function: " << x; }
Output
Value of x from my_function: 50 Value of x from main function: 10
Call by Reference
In call by reference the actual value that is passed as argument is changed after performing some operation on it. When call by reference is used, it creates a copy of the reference of that variable into the stack section in memory. Is uses a reference to get the value. So when the value is changed using the reference it changes the value of the actual variable.
Example Code
#include<iostream> using namespace std; void my_function(int &x) { x = 50; cout << "Value of x from my_function: " << x << endl; } main() { int x = 10; my_function(x); cout << "Value of x from main function: " << x; }
Output
Value of x from my_function: 50 Value of x from main function: 50
Where to use Call by reference?
The call by reference is mainly used when we want to change the value of the passed argument into the invoker function.
One function can return only one value. When we need more than one value from a function, we can pass them as an output argument in this manner.
- Related Articles
- Describe pass by value and pass by reference in JavaScript?
- Pass by reference vs Pass by Value in java
- What is Pass By Reference and Pass By Value in PHP?
- Is java pass by reference or pass by value?
- Which one is better in between pass by value or pass by reference in C++?
- Is JavaScript a pass-by-reference or pass-by-value language?
- Pass by reference vs value in Python
- What is the difference between pass by value and reference parameters in C#?
- Pass an integer by reference in Java
- What is pass by reference in C language?
- How to pass an array by reference in C++
- How to pass arguments by reference in Python function?
- How do you pass objects by reference in PHP 5?
- How to pass arguments by reference in a Python function?
- Why do we pass a Pointer by Reference in C++?
