Difference Between Call by Value and Call by Reference

Difference Between Call by Value and Call by Reference

7 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 21, 2024 17:16 IST

Have you ever wondered how a function in your code affects the variables you pass to it? The difference between Call by Value and Call by Reference is a fundamental concept that determines whether a function modifies your original data or just works with a copy of it. Let us read more about it!

Call by Value and Call by Reference are two methods of passing arguments to functions in programming. In this blog, we will understand the difference between call by value and call by reference in detail.

Table of Content

 
Recommended online courses

Best-suited Programming courses for you

Learn Programming with these high-rated online courses

β‚Ή3.02 L
36 months
β‚Ή3.15 L
4 years
Free
6 weeks
β‚Ή7.1 K
6 weeks
Free
10 hours
Free
8 weeks
β‚Ή1.24 L
48 months
Free
15 weeks
β‚Ή32 K
1 year
– / –
350 hours

Difference Between Call by Value and Call by Reference

Below is a table showing the difference between call by value and call by reference.

Aspect

Call by Value

Call by Reference

Naming Convention

Named so because it involves passing the actual values of arguments to the function.

Named so because it involves passing the reference (address) of arguments to the function.

Effects of Changes

Changes in the function to the parameters do not affect the original arguments.

Changes in the function to the parameters directly affect the original arguments, as they are references.

Type of Passing

Passes a copy of the variable's value.

Passes the variable itself, meaning the address of the variable is passed.

Memory Location

Uses different memory locations for actual arguments and parameters (formal and actual arguments are created in different memory locations).

Uses the same memory location for actual arguments and parameters (formal and actual arguments are created in the same memory location).

Language Support

Supported by languages like C++, C#, PHP, Visual Basic NET, etc., often as the default method.

Supported by C++ (with references). Java uses a Call by Value approach where object references are passed by value, not the objects themselves.

Value Modification

No modification in the original value.

Modification occurs in the original value.

Internal Implementation

Involves copying the value of the parameter to the function's local argument.

Involves copying the address of the parameter to the function's local argument, leading to shared memory location.

Method of Passing

Uses a straightforward method or simple technique for passing values.

Requires defining pointer variables (or equivalent) to store the address of variables.

Manipulation of Variables

Changes to dummy variables in the function do not affect the actual variables in the calling function, preventing manipulation of actual variables through function calls.

Allows for direct manipulation of actual variables through function calls, as both refer to the same memory location.



What is Call by Value?

Call by Value is a method of passing arguments to a function where the actual value of the argument is copied into a separate location accessible by the function. When the function operates on these arguments, it does so on their copies, so any changes made within the function do not affect the original arguments outside the function. This is a standard approach in many programming languages, especially for primitive data types like integers, floats, and characters.

Mind Map for Call by Value

 

Let's see an example in C programming language below to understand the concept better.


 
#include <stdio.h>
// Function to add 10 to the given number
void addTen(int number) {
number = number + 10;
printf("Inside function: %d\n", number);
}
int main() {
int originalNumber = 5;
// Function call does not change the originalNumber
addTen(originalNumber);
printf("Outside function: %d\n", originalNumber);
return 0;
}
Copy code

Output

Inside function: 15
Outside function: 5

In the example above,

  • The main function defines a variable originalNumber with the value 5.
  • It then calls the addTen function, passing originalNumber as an argument.
  • Inside the addTen function, the parameter number is a copy of originalNumber. Any operation inside addTen is performed on this copy. Here, it adds 10 to number and prints the result.
  • However, once the control returns to main, originalNumber remains unchanged, as demonstrated by the final print statement.

In Call by Value, the function addTen does not have access to the actual variable originalNumber. It only works with a copy, so the original variable remains unaffected by whatever happens inside the function. This makes Call by Value a safer method when you want to ensure that the original values of variables are not altered.

What is Call by Reference?

Call by Reference is a method of passing arguments to a function where instead of passing the values of the variables, the function receives references to the actual variables. This means that the function operates directly on the original variables, and any changes made within the function are reflected in those variables outside the function. This method is commonly used in languages like C++, where you can pass references or pointers to achieve this effect.

Mind Map for Call by Reference

 

Let's see an example in C++ below to understand the concept better.


 
#include <iostream>
using namespace std;
// Function declaration with a reference parameter
void addTen(int &number) {
// Modify the parameter, which affects the original variable
number = number + 10;
cout << "Inside function: " << number << endl;
}
int main() {
// Initialize an integer variable
int originalNumber = 5;
// Call the function with a reference to the variable
addTen(originalNumber);
// Print the value after the function call to show that it has been changed
cout << "Outside function: " << originalNumber << endl;
return 0;
}
Copy code

Output

Inside function: 15
Outside function: 15

In the example above,

  • The addTen function takes an integer reference (int &number) as its parameter.
  • When addTen is called in the main function, it is passed the reference to originalNumber.
  • Inside addTen, any modification to number directly affects originalNumber because number is a reference to originalNumber.
  • After the function call, originalNumber reflects the changes made inside addTen.

This demonstrates how Call by Reference allows a function to modify the original variables passed to it, in clear contrast to Call by Value where such modifications are not possible.

Similarities Between Call by Value and Call by Reference

Below is a Table highlighting the key similarities between call by value and call by reference.

Aspect

Call by Value and Call by Reference

Function Call

Both methods are used when calling a function, though they differ in what is passed (value or reference).

Purpose

Both are intended to supply data from the calling environment to a function for processing, enhancing modularity and reusability in programming.

Top OOPs Interview Questions and Answers (2023)
Top OOPs Interview Questions and Answers (2023)
Object-oriented programming (OOP) is a paradigm that uses objects containing data in programming. It has become a fundamental part of software development. Many companies hire professionals who have a good...read more

Top 160+ Java Interview Questions and Answers for 2024
Top 160+ Java Interview Questions and Answers for 2024
This article consists of the TopΒ 160 Java interview questions and answers, covering all the important topics such as Java features, Core Java concepts, OOPs concepts, collections, access specifiers, threads, exceptions,...read more

Top C++ Interview Questions and Answers for 2024
Top C++ Interview Questions and Answers for 2024
C++ is a popular object-oriented programming (OOP) language. Due to its reliability, speed, compatibility, and performance, it is widely used in the development of operating systems, applications, games, servers, and...read more

Top 110+ Python Interview Questions and Answers
Top 110+ Python Interview Questions and Answers
Python is a widely-used general-purpose, object-oriented, high-level programming language. It is used to create web applications, and develop websites and GUI applications. The popularity of the language is due to...read more

Top 80+ C Programming Interview Questions and Answers
Top 80+ C Programming Interview Questions and Answers
Here’s a list of 80+ top frequently asked C programming interview questions and answers to help you crack your next interview. Preparing for a C programming interview? Here are the...read more

Thus, the concepts of Call by Value and Call by Reference are fundamental in understanding how functions interact with arguments in programming. While they share the common purpose of passing data to functions and supporting modularity in code design but they differ significantly in their implementation and effects.

FAQs

What is the main difference between Call by Value and Call by Reference?

Call by Value means that a copy of the actual value is passed to the function, so modifications made to the parameter inside the function do not reflect outside of it. Call by Reference, on the other hand, means a reference (or pointer) to the actual parameter is passed, so changes made inside the function do affect the original argument.

Does Call by Value provide more security compared to Call by Reference?

Yes, Call by Value is generally more secure as it passes a copy of the data, ensuring that the original data cannot be accidentally modified or corrupted. In contrast, Call by Reference can lead to unintended side effects on the original data.

Which is faster, Call by Value or Call by Reference?

Call by Reference is usually faster for large data structures or objects, as it avoids the overhead of copying large amounts of data. However, for small or primitive data types, Call by Value can be equally efficient or sometimes faster due to the overhead of accessing memory locations in Call by Reference.

Are there any specific use cases where one method is preferred over the other?

Call by Value is preferred when the integrity of the original data must be maintained without any risk of modification. Call by Reference is more useful when working with large data structures where performance is a concern, or when the function needs to modify the original data.

How do different programming languages implement these concepts?

Languages like C and C++ offer both Call by Value and Call by Reference (though in C, Call by Reference is simulated using pointers). Languages like Java use Call by Value for primitives and Call by Reference for objects. In contrast, Python and JavaScript use a model called Call by Sharing, which is often confused with Call by Reference.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio