Call by Value in C++

In Call by Value, when a function is called, the value of the actual parameter is copied into the formal parameter. This means changes made to the formal parameter inside the function do not affect the actual parameter. It ensures data protection, as the original data remains unchanged.

Let us also understand what are the two types of parameters:

  1. Actual parameters are the values or variables passed to the function when it is called.
  2. Formal parameters are the variables defined in the function declaration and definition that receive the values of the actual parameters.

Let us see an example to implement Call by Value in C++:

Here is the output:

The above C++ code demonstrates a call-by-value mechanism. The changeValue function changes the value of its parameter x to 10, but this change does not affect the actual argument a in the main function. Before calling changeValue, the code prints the value of a. After the function call, it prints the value of a again, showing it remains unchanged (still 5).

In Call by Value, a copy of the actual parameter’s value is passed to the function. Changes made to the parameter inside the function do not affect the original value outside the function.


If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

break and continue in C++
Call by Reference in C++
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading