Introduction
Since we all understand that functions are an essential part of any program/code written in any programming language, there are various kinds of function calling in any programming language. one of its roles is the argument calling function. In this kind, we can see multiple types of classes such as Call by value, call by function, call by the result, etc. here, we will be speaking about the two important ones and comparing them.
Also see, Literals in C, Fibonacci Series in C++
Call By Value in C++
In Call by value, the original value isn't permanently changed by using value.
In Call by value, the value being passed to the function is locally stored using the function parameter in the stack memory location. If you change the value of the function parameter, it is modified for the current function only. It will now not change the variable's value inside the caller method, such as main().
Example
#include <iostream>
using namespace std;
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
int main ()
{
int a = 10;
int b = 20;
cout << "Before swap, the value of a :" << a << endl;
cout << "Before swap, the value of b :" << b << endl;
swap(a, b);
cout << "After swap, the value of a :" << a << endl;
cout << "After swap, the value of b :" << b << endl;
return 0;
}
Output:
Before swap, the value of a:10
Before swap, the value of b:20
After the swap, the value of a:10
After the swap, the value of b:20
You can try by yourself with the help of online c++ compiler.