How the value is passed in C++
Introduction:
The call-by-value method of giving arguments to a function duplicates the real value of an argument into the formal parameter of the function. In this instance, modifications to the parameter inside the function have no impact on the argument.
Call by value is the default method for passing arguments in C++. This indicates that code inside a function cannot change the arguments used to invoke the function.
Call By Value:
In C++, the call-by-value method is used to give arguments or parameters to a function, which duplicates the function's initial values to the passed parameters.
The given parameters are unaffected by changes made to the function's internal parameters. Hence the C++ call-by-value approach is used by default for calling arguments that cannot be constantly changed.
The values provided to the main function cannot be changed by any functions included in the code. After supplying parameters outside of the main function, the values may also be changed.
How does Call by Value work in C++?
The formal parameter given within the function is copied when using the call-by-value technique to pass an argument. However, the arguments given have some impact on the function, which states that modifications made to a copy of the variable do not interfere with and do not change the value of the variable outside of the method. Actual and formal parameters provided to the function do not change the value and pass the values of the variables in a very straightforward and easy way.
Furthermore, the real value, or original value, is never affected or altered. But the question is where these arguments are being transmitted or how it works with memory management strategies. Actual and formal parameters are created in many locations throughout memory, and each of these locations has a unique set of permissions for accessing the parameters or the function.
When C++ values are used in programming languages like C++, PHP, etc., they always have their default values and are never altered or regularly updated. With a call by reference, however, the scenario is quite the reverse because the parameters or values given are altered with various types of values and transformations.
It is easy to say that the call-by-reference method in C++ is less secure than the call-by-value technique since the original value changes and is susceptible to modification there. When calling by reference instead of calling by value and allocating memory, memory is allocated at the time the method is being executed.
Advantages of Call by Value in C++:
There are benefits and drawbacks to every function, and in the case of a call-by-value method, the main benefits are as follows:
- Since the arguments passed do not have the authority to alter the values, the method that contains the data is not changed.
- The real or formal parameters do not change when the function is called, indicating that the change does not impair functioning.
- The most intriguing benefit is that none of the formal parameters are changed by the input supplied to the function. Therefore there isn't much of a difference.
- Protecting the whole function and its data from any security breaches gives programmers protection and a safer method of working.
Program:
#include <iostream>
using namespace std;
// Function Definition
void swap(int x, int y ) {
int temp;
temp = x;
x = y;
y = temp;
}
int main()
{
// initialize variables
int a = 12, b = 14;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
// calling a function to swap the values
swap(a, b);
cout << "After swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
Explanation:
- 1st we define a function whose name is "swap" in the function parameter; we have two formal arguments, "x" and "y," which have an int data type.
- In the swap function body, we have three variables "temp", "x," and "y".
- The "temp" variable is initialized with the "x" variable, "x" is initialized with the "y" variable, and the "y" variable is initialized with the "temp" variable.
- In the main() function body, we have two variables, "a" and "b," which are initialized with 12 and 14, respectively.
- First of all, execution will start from the main() function.
- Then the line "before swapping" is executed first, and values of "a" and "b" will print.
- Then swap() function is called. In this called function, we have two actual arguments, "a" and "b," which will pass their values to the formal arguments “x” and “y”.
- But when this swap() function execution is finished, all the data will also vanish with the swap() function and the main() function gets control back.
- So whatever changes happen inside the swap function, these changes will also delete with the function ending, and then we do not get swapping in the number.
Program Output:
Program:
#include <iostream>
#include <iostream>
using namespace std;
// Function to change the value of a
int sum(int a)
{
a += 2;
return a;
}
int main()
{
int a = 4;
cout << "Value of a before modification: " << x << endl;
a = sum(a);
cout << "Value of a after modification: " << x << endl;
return 0;
}
Explanation:
- 1st we define a function "sum" in which we have one formal argument "a," which has an int data type.
- In the sum() function body, we increment the “a” variable by 2.
- In the main() function, we declare a variable "a," which has an "int" data type, and we initialize "a" with 4.
- First, execution will start from the main() function, then the line "value of a before modification" is executed first.
- Then sum() function will be called, and the execution of the sum() function will start. The value of "a" will increase by 2, and this value will store in variable "a."
- And when the execution of the sum() function will finish, the main() function gets control back.
- Then the line “value of a after modification” will execute.
Program Output:
Conclusion:
Given how frequently this technique is used in the everyday code base, the call-by-value method in C++ plays a crucial role in any programming language. Because most programs are supported by programmers by default, it gives programmers the flexibility of reusability and time flexibility.