Pass by Value

When a parameter is passed into a function by value, the compiler takes a copy of that passed in value for ongoing use within the function and any execution upon that value is local to that function only, i.e. it does not effect the passed in value by the caller outside of the function.


Upon completion of the function, any changes to the copied in value are lost.


The passed in values do not have to use the same identifier name as those outside of the function. e.g. a and b in main() can be called x and y within the function. What matters is the order in which they're used and their type:

#include <iostream>
using namespace std;

void myFunc(int x, int y) {

	x *= x ;
	y *= y ;

	cout << "x and y in myFunc: " << x << ", " << y << endl ;

int main () {

	int a = 17, b = 42 ;

	myFunc(a, b) ;

	cout << "a and b in main: " << a << ", " << b << endl ;

	return 0;

Compile & Run:

x and y in myFunc: 289, 1764
a and b in main: 17, 42

Leave a Reply