Pass by Reference in C++ functions

By: Grant Braught Viewed: 169 times  Printer Friendly Format    


This sample C++ program, demonstrates Pass by Reference functions.
#include 

// Function prototypes...
void Func1(int Num);
void Func2(int &Num);

int main(void)
{
	int A = 3;
	
	cout << "Before Func1: A = " << A << endl << endl;
	
	Func1(A);
	
	cout << "After Func1:  A = " << A << endl << endl;

	Func2(A);
	
	cout << "After Func2:  A = " << A << endl;
	
	return(0);
}


void Func1(int Num)
{	// Function that adds 2 to its 
	// Pass-By-Value parameter...
	
	Num = Num + 2;
}

void Func2(int &Num)
{	// Function that adds 2 to its 
	// Pass-By-Reference parameter...

	Num = Num + 2;
}


Most Viewed Articles (in C++ )

Latest Articles (in C++)

Comment on this tutorial