Sample – understanding call-by-value and call-by-reference

by Nideesh C on February 8, 2011 · 0 comments

in C++




// understanding call-by-value vs call-by-reference

#include <iostream.h>

int f1 (int, int, int);
int f2 (int&, int&, int&);
int f3 (int, int&, int);

void main () {
int i, j, k;

i=1;
j=2;
k=3;

cout << endl;
cout << “Initial values of i, j, and k are: ”
<< i << “, ” << j << “, and ” << k << endl << endl;
cout << “f1(i,j,k) = ” << f1(i,j,k) << endl;
cout << “Values of i, j, and k after the call to f1 are: ”
<< i << “, ” << j << “, and ” << k << endl << endl;
cout << “f2(i,j,k) = ” << f2(i,j,k) << endl;
cout << “Values of i, j, and k after the call to f2 are: ”
<< i << “, ” << j << “, and ” << k << endl << endl;
cout << “f3(i,j,k) = ” << f3(i,j,k) << endl;
cout << “Values of i, j, and k after the call to f3 are: ”
<< i << “, ” << j << “, and ” << k << endl;
}

int f1 (int x, int y, int z) {
x=x+5;
y=y+5;
z=z+5;
return(x+y+z);
}

int f2 (int& x, int& y, int& z) {
x=x+5;
y=y+5;
z=z+5;
return(x+y+z);
}

int f3 (int x, int& y, int z) {
x=x+5;
y=y+5;
z=z+5;
return(x+y+z);
}

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. Sample – check if a number is prime
  2. Sample program (multiplication using addition)
  3. Sample – exponentiation using multiplication
  4. Sample – factorial
  5. Sample – Fibonacci numbers

Leave a Comment

Previous post:

Next post: