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 << "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

 Live Demo

#include 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.

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

Updated on 30-Jul-2019 22:30:25

  • Related Questions & Answers
  • Describe pass by value and pass by reference in JavaScript?
  • Pass by reference vs Pass by Value in java
  • Is java pass by reference or pass by value?
  • What is Pass By Reference and Pass By Value in PHP?
  • Is JavaScript a pass-by-reference or pass-by-value language?
  • Which one is better in between pass by value or pass by reference in C++?
  • 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
  • Explain javascript pass by reference in javascript?
  • What is pass by reference in C language?
  • How to pass arguments by reference in Python function?
  • How to pass an array by reference in C++
  • Pass an array by value in C
  • Explain javascript pass by value in javascript?

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Differences Between Passing an Argument By Value and By Reference (Visual Basic)

  • Article
  • 09/15/2021
  • 2 minutes to read

In this article

When you pass one or more arguments to a procedure, each argument corresponds to an underlying programming element in the calling code. You can pass either the value of this underlying element, or a reference to it. This is known as the passing mechanism.

Passing by Value

You pass an argument by value by specifying the ByVal keyword for the corresponding parameter in the procedure definition. When you use this passing mechanism, Visual Basic copies the value of the underlying programming element into a local variable in the procedure. The procedure code does not have any access to the underlying element in the calling code.

Passing by Reference

You pass an argument by reference by specifying the ByRef keyword for the corresponding parameter in the procedure definition. When you use this passing mechanism, Visual Basic gives the procedure a direct reference to the underlying programming element in the calling code.

Passing Mechanism and Element Type

The choice of passing mechanism is not the same as the classification of the underlying element type. Passing by value or by reference refers to what Visual Basic supplies to the procedure code. A value type or reference type refers to how a programming element is stored in memory.

However, the passing mechanism and element type are interrelated. The value of a reference type is a pointer to the data elsewhere in memory. This means that when you pass a reference type by value, the procedure code has a pointer to the underlying element's data, even though it cannot access the underlying element itself. For example, if the element is an array variable, the procedure code does not have access to the variable itself, but it can access the array members.

Ability to Modify

When you pass a nonmodifiable element as an argument, the procedure can never modify it in the calling code, whether it is passed ByVal or ByRef.

For a modifiable element, the following table summarizes the interaction between the element type and the passing mechanism.

Element typePassed ByValPassed ByRef
Value type (contains only a value) The procedure cannot change the variable or any of its members. The procedure can change the variable and its members.
Reference type (contains a pointer to a class or structure instance) The procedure cannot change the variable but can change members of the instance to which it points. The procedure can change the variable and members of the instance to which it points.

See also

  • Procedures
  • Procedure Parameters and Arguments
  • How to: Pass Arguments to a Procedure
  • Passing Arguments by Value and by Reference
  • Differences Between Modifiable and Nonmodifiable Arguments
  • How to: Change the Value of a Procedure Argument
  • How to: Protect a Procedure Argument Against Value Changes
  • How to: Force an Argument to Be Passed by Value
  • Passing Arguments by Position and by Name
  • Value Types and Reference Types

Feedback

Submit and view feedback for

What is the difference between passing argument by value and by address in C?

While calling a function, we pass values of variables to it. Such functions are known as “Call By Values”. While calling a function, instead of passing the values of variables, we pass address of variables(location of variables) to the function known as “Call By References.

What is the difference between call by value and call by reference in C?

In the case of Call by Value, when we pass the value of the parameter during the calling of the function, it copies them to the function's actual local argument. In the case of Call by Reference, when we pass the parameter's location reference/address, it copies and assigns them to the function's local argument.

What is the difference between pass by value by reference in C and pass by reference in C ++?

Unlike in C, where passing by reference was really just passing a pointer by value, in C++ we can genuinely pass by reference.

What is the difference between passing arguments by value and passing arguments by reference?

The main difference between pass by value and pass by reference is that, in a pass by value, the parameter value copies to another variable while, in a pass by reference, the actual parameter passes to the function.