Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Call By Value in C++
2.1.
Example
3.
Call By Reference in C++
3.1.
Example
4.
Difference Between the Call By Value and Call By Reference
5.
Frequently Asked Questions
6.
Key Takeaways
Last Updated: Sep 11, 2024

Call by value and Call by reference in C++

Introduction

Since we all understand that functions are an essential part of any program/code written in any programming language, there are various kinds of function calling in any programming language. one of its roles is the argument calling function. In this kind, we can see multiple types of classes such as Call by value, call by function, call by the result, etc. here, we will be speaking about the two important ones and comparing them.


Also see, Literals in C, Fibonacci Series in C++

Call By Value in C++

In Call by value, the original value isn't permanently changed by using value.

In Call by value, the value being passed to the function is locally stored using the function parameter in the stack memory location. If you change the value of the function parameter, it is modified for the current function only. It will now not change the variable's value inside the caller method, such as main().

Example

#include <iostream>
using namespace std;
 void swap(int x, int y) 
{
   int temp;
   temp = x;
   x = y;   
   y = temp; 
   return;
}
int main () 
{
   int a = 10;
   int b = 20;
   cout << "Before swap, the value of a :" << a << endl;
   cout << "Before swap, the value of b :" << b << endl;
   swap(a, b);
   cout << "After swap, the value of a :" << a << endl;
   cout << "After swap, the value of b :" << b << endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Before swap, the value of a:10
Before swap, the value of b:20
After the swap, the value of a:10
After the swap, the value of b:20
You can also try this code with Online C++ Compiler
Run Code

 

You can try by yourself with the help of online c++ compiler.

Call By Reference in C++

Call by reference method copies the address of an argument into the formal parameter. In this method, the address is used to access the real argument used in the function call. It means that changes made inside the parameter adjust the passing argument.

In this method, memory allocation is the same as the real parameters. All the operations in the function are finished on the value saved at the address of the actual parameter, and the modified value could be stored on the same address.

Example

#include <iostream>
using namespace std;
void swap(int &x, int &y) 
{
   int temp;
   temp = x; 
   x = y;   
   y = temp; 
   return;
}
int main () 
{:
   int a = 10;
   int b = 20;
   cout << "Before swap, the value of a :" << a << endl;
   cout << "Before swap, the value of b :" << b << endl;
   swap(a, b);
   cout << "After swap, the value of a :" << a << endl;
   cout << "After swap, the value of b :" << b << endl;
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output:

Before swap, the value of a:10
Before swap, the value of b:20
After the swap, the value of a:20
After the swap, the value of b:10
You can also try this code with Online C++ Compiler
Run Code

Difference Between the Call By Value and Call By Reference


1. Call by value is treated as an asset, whereas Call by reference is treated as a liability.

2. Program in Call by using value is easily understandable and compact, whereas the program in Call by reference is more complex and large.

3. Every problem cannot be solved by Call by means of value method, while every problem can be solved by Call by reference method.

4. For large and redundant value calling programs, call-by-reference is preferable.

5. In Call by value, actual arguments will remain safe. They can not be modified/changed accidentally. In Call by reference, alteration to basic ideas is possible within from called function; therefore, the code must manage arguments carefully or get unexpected results.

Frequently Asked Questions


1. What do you know about the memory location of Call by value and Call by reference?

Actual and formal arguments could be created in a different memory location in Call by value. Actual and formal statements will be created in the same memory location in Call by reference.

2. What do you know about the passing of variables in Call by value and Call by reference?

Values of variables are exceeded using a sincere technique in Call by value. Pointer variables are required to save the address of variables in Call by reference.

3. What do you know about the Default parameter in Call by value and Call by reference?

Default in many programming languages like C++, PHP. simple visual net, and C# in Call by value. Call by reference is supported by most programming languages like JAVA, but not as default.

Key Takeaways

After reading the blog on Call by value and Call by reference in C++, We'll have understood those methods and how both of these methods work, along with a few examples. We also learned the difference between Call by value and Call by reference methods. You can learn more about such programming concepts here.

Recommended Readings: 

Live masterclass