Programming Tutorials

Pass by Reference in C++ functions

By: Grant Braught in C++ Tutorials on 2011-01-27  

This sample C++ program, demonstrates Pass by Reference functions.
#include <iostream.h>

// 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;
}





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)