×

C++ Call by Value

In this article, we will discuss C++ Call by Value with their syntax, examples, use cases, advantages, and disadvantages.

C++ Function

A function is a set of statements that executes a task. It executes only when it is called. In C++, functions are categorized into two different categories on behalf of argument (parameter) pass.

  • Function with parameter (return_type function_name(paramenter);)

A function that takes a parameter in a function is called a function with a parameter.

  • Function without parameter (return_type function_name();)

A function that does not take a parameter in the function is called a function without a parameter. Here, we can see the working of C++ functions.

Function with a parameter has further categorized into two different categories. These categories are Call by Value and Call by Reference. Here, we will discuss only Call by Value.

C++ Call by Value

When a function is called, new elements are created in the stack memory to store essential data about the function, including allocated memory for parameters, local variables, and the return value. The Call by value method enables us to copy an actual parameter into a formal parameter. If we wish to modify the value of the function parameter, it is changed only for the current function. It means that any changes nside the parameter do not affect the original variables. It is also known as pass by copy or call by value.

Example:

Let us take an example to illustrate the Call by Value in C++.

#include<iostream>

using namespace std;

void swap(int,int);

int main()

{

int a=5,b=10;

cout<<"Before swapping
Value of a is :"<<a<<"
Value of b is :"<<b;

swap(a,b);

cout<<"
Outside function after swapping
Value of a is :"

<<a<<"
Value of b is "<<b;

return 0;

}

void swap(int a,int b)

{

int temp;

temp=a;

a=b;

b=temp;

cout<<"
Value inside function after swapping
Value of a is

:"

<<a<<"
Value of b is :"<<b;

}

Output:

Before swapping

Value of a is : 5

Value of b is: 10

Value inside function after swapping

Value of a is :10

Value of b is :5

Outside function after swapping

Value of a is : 5

Value of b is: 10

Explanation:

Before the Function Call:

  • In this example, we declared two integer variables, a and b and initialized them with values 5 and 10.

Inside the swap() function:

  • A copy of a and b is passed to the swap() function.
  • temp stores a (5).
  • a is assigned b (10).
  • b is assigned temp (5).
  • Swapped values (a = 10, b = 5) are printed inside the function.

After the Function Call:

  • Since the function used Call by Value, the original a and b in the main() function remain unchanged.
  • The output still shows a = 5 and b = 10.

Example 2:

Let us take another example to illustrate the Call by Value in C++.

#include <iostream>

using namespace std;


int fun(int a)

{

    a += 18;

    return a;

}


int main()

{

    int a = 2;


    cout << "Before function Calling the value of A: " << a << endl;


    a = fun(a);

    cout << "After function Calling the value of A: " << a << endl;


    return 0;

}

Output:

Before function Calling the value of A: 2

After function Calling the value of A: 20

Uses of Call by Value in C++:

Several uses of Call by Value in C++ are as follows:

  1. It is mainly utilized for passing simple data types, including integers, Boolean, floats, and characters.
  2. It is very useful when we do not wish to alter the actual parameters of the function.
  3. It is also useful when we do not deal with backtracking or recursion.
  4. When we wish to create copies of essential data.
  5. It is useful when we wish to perform calculations without changing the actual parameter values.
  6. It is very important when multiple threads access the shared data.
  7. It prevents one function call from modifying another value.

Advantages of Call by Value:

Several advantages of Call by Value in C++ are as follows:

  1. It is easy to understand and debug because values do not change unexpectedly.
  2. Call by value functions are free to alter the passed data without affecting the actual data.
  3. It is useful in parallel processing when multiple threads work on the same function.
  4. There is no risk of memory leaks or dangling pointers.
  5. Because the function cannot alter the actual data, it aids in preventing unintended changes.
  6. When a function is called, it does not affect the actual data of the actual parameter.

Disadvantages of Call by Value:

Several disadvantages of Call by Value in C++ are as follows:

  1. If the parameter is huge, it takes a lot of time to copy the data.
  2. It does not work effectively with dynamic memory allocation.
  3. Each function call makes a new copy in recursive functions, which consumes more stack memory.
  4. Huge memory space is required while using array, vector, string, etc.
  5. If a function is invoked too many times using Call by Value, new copies are made for each call, resulting in a stack overflow.

Frequently Asked Questions (FAQ’s)

1. What is Call by Value?

Call by Value is a function of passing arguments to a function where copies of the original values are passed. It means that any changes inside the parameter do not affect the original variables.

2. Does Call by Value work with arrays in C++?

No, Arrays are only passed by reference in C++.

3. Does Call by Value work with objects?

Yes, but it is inefficient because it makes a copy of the complete object.

4. Is Call by Value faster than Call by Reference?

No, Call by Value is slower for large data types (structs, classes, arrays) because it makes copies of actual parameters. Call by Value is fast for simple data types (int, char, boolean, float) because they are small in size.


Related Topics

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

sort() function in C++

This tutorial covers the various built-in sort functions found in the C++ algorithm’s library.  What Does C++ Sort Mean? The concept of sorting in C++ entails rearranging an array's elements in a...

3 minutes read.

How to Reverse a String in C++ using Do-While Loop

Strings In C++, a string is an object that represents a group (or sequence) of various characters. Strings are part of the standard string class in C++ (std::string). The characters of...

4 minutes read.

Destructor

Let’s discover the C++ destructor, virtual and pure virtual destructors, and when destructors are invoked and their rules in this lesson. Destructors : A member function that destroys an object is known as...

5 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

How to make a password program in C++

Before understanding the password program, one must know about a password and why it is required. Password: A password is a word that permits access to somewhere or something. A password...

4 minutes read.

C++ Algorithms

There are plenty of programming paradigms that are closely associated with the implementations of code and simulate them into a proper functional one. This is done with the help of...

5 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Scope Resolution Operator in C++

The scope resolution operator and its different usage in the C++ programming language will be discussed in this section. The scope resolution operator is used to refer to an out-of-scope...

6 minutes read.

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

DES in C++

The popularity of the Data Encryption Standard (DES) is slightly declining as a result of the discovery that DES is susceptible to very strong attacks. Since DES is a block cypher,...

3 minutes read.

C++ Expressions

C ++ equations are made up of operators, constants and variables arranged according to language rules. It may also include function calls that give results. To calculate the value, the...

4 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.