Sunday, September 11, 2011

Pass argument to function by value and by reference

example:

Pass argument to function by value and by reference

#include <iostream>

using namespace std;

void function(int byValue, int& byRef);

int main()
{
int a = 5;
int b = 10;
cout << endl << "before fnction()" << endl;
cout << "a (byValue) = " << a << endl;
cout << "b (byRef) = " << b << endl;
function(a, b);

cout << endl << "after fnction()" << endl;
cout << "a (byValue) = " << a << endl;
cout << "b (byRef) = " << b << endl;
return 0;
}

void function(int byValue, int& byRef){
byValue++;
byRef++;
}

No comments:

Post a Comment