Call by value function in C++ wtih example


When a function is called, any arguments that are provided by the caller are simply treated as expressions in programming languages. The value of each expression has the appropriate conversion applied and is then used to initialize the corresponding formal parameter in the called function, that behaves in exactly the same way as any other local variables in the function. Calling a function by passing the value of an actual argument is called as call by value.

C++ Program to call by value function

#include<iostream> using namespace std; void swap(int a, int b){ int z; z = a; a=b; b=z; } int main(){ int a = 33, b = 35; cout<<"Before :"<<a<<","<<b<<endl; swap(a,b); cout<<"After :"<<a<<","<<b; return 0; }

Output:-

Before :33,35
After :33,35

Explanation of the program

Here, the function is called by the value which means that the value of the arguments is passed on to the function.