paint-brush
Call-By-Value and Call-By-Referenceby@supernerdd007
611 reads
611 reads

Call-By-Value and Call-By-Reference

by Rohan Rai3mFebruary 22nd, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

There are two ways to pass data to the function in C/C++. Pass-By-Value and Pass-by-Reference are equally important features of C/CC++. The time complexity of programs using pass-by value and pass-By reference is almost similar. When to use Pass-BY-Value is when we want to calculate the value of a particular function without modifying the values of arguments passed. To avoid bugs caused due to memory stack issues, use pass-via-reference to update a variable's value.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Call-By-Value and Call-By-Reference
Rohan Rai HackerNoon profile picture

Abstract

Passing data to a function is very important and is often used at times in writing the code.

  • There are two ways to pass data to the function in C/C++.
  • Pass-By-Value.
  • Pass-By-Reference.

Scope of the Article

  • This article will provide you with a basic to intermediate level of understanding of how to pass data to the function.
  • Covers the concepts like Call-By-Value & Call-By-Reference using code snippets in C/C++.

Note

For the scope of this article, we will use Call-By-Value and Pass-By-Value interchangeably similar is the case for Call-By-Reference and Pass-By-Reference. You can learn more about the differences here.

Prerequisites

  • One should have a basic understanding of C/C++.
  • How to make Functions in C/C++ with a variable number of arguments.
  • How to pass different parameters to the function.

Now, If You Know the Prerequisites, you can move forward.

Pass-By-Value

  • Pass by value is when a function copies the actual value of an argument into a formal parameter of the function.
  • Changes made to the parameter in the function have no effect on the argument after the function call ends.
  •  In Simpler terms, we can say that changes made to the variable passed by value to the respective function will only reflect in the particular scope of that function.
  • Let's look at a few examples to clarify this.

Example Code 1

#include<bits/stdc++.h>
void add(int x,int y)
{
      x=x+y;
      return;
}
int main()
{
       
int x=5;
int y=6;
 
add(x,y); // passing by value the parameters (x,y). 
 
cout<<x<<"\n";
return 0;

What do you think the value of x will be?

A.)  5

B.) 11

Notice one thing carefully: we have passed the parameters x and y with value. Meaning the add function only has a copy of their value, not the memory address.

So the update of (x=x+y) has a local scope inside the add function only. After the function call ends, the value of variable x will be the same as the previous, i.e., 5.

Example Code 2

#include<bits/stdc++.h>
using namespace std;
 
void swapValues(int x,int y)
{
    swap(x,y);
      return;
}
int main()
{
       
int x=5;
int y=6;
 
swapValues(x,y); // passing by value the parameters (x,y). 
 
cout<<x<<"\n";
return 0;
}

When to use Pass-By-Value

  • When we want to calculate the value of a particular function without modifying the values of arguments passed.
  • To avoid bugs caused due to memory stack issues.

Pass-By-Reference

  • Pass by reference copies the address of an argument into the formal parameter.
  • The address is used to access the actual argument used in the call.
  • This means the changes made to the parameter affect the passed argument.

Example Code

#include<bits/stdc++.h>
using namespace std;
 
// Taking the values of x and y with reference.
void swapValues(int &x,int &y)
{
    swap(x,y);
      return;
}
int main()
{
       
int x=5;
int y=6;
 
swapValues(x,y); // passing by reference the parameters (x,y). 
 
cout<<x<<"\n";
return 0;
}

Here notice one thing carefully: we have taken the values of x and y using the ampersand operator (&), meaning we are taking values by reference using the actual memory address of the variables.

So after the function calls end. The value of x will be swapped with the value of y.

When to use Pass-By-Reference

  • Whenever you want to store the modified value of a variable after the function call ends.
  • If you need to update the value of some variable in a recursive function.

Time Complexity

The time complexity of programs using pass-by-value or pass-by-reference is almost similar.

Key Points and Conclusion

  1. Both are equally important features of C/C++ pass-by-value and are mostly used over pass-by-reference.
  2. A copy of the same variable is stored in the call stack during the function call.
  3. As per the needs of the program, the choice between pass-by-value and pass-by-reference has to be made.

Thank you so much for reading this!

The featured image is taken from here.