+ 1
Program to illustrate call by reference
2 Answers
+ 16
//===================================
// different kind of function calls
// Done for : Divya Sharma
// By : Babak Sheykhan
//===================================
#include <iostream>
using namespace std;
void pass_by_val_1(int n) {
++n;
}
void pass_by_val_2(int &n) {
// consider reference
++n;
}
void pass_by_ref(int *n) {
++*n;
}
void pass_by_pointer(int *n) {
++*n;
}
int main()
{
int a, b, c, d;
a = b = c = d = 1;
// declare and initialize an integer pointer by d address
int *dPtr = &d;
// function calls
pass_by_val_1(a); // no effect on a
pass_by_val_2(b); // has effect on b
pass_by_ref(&c); // has effect on c
pass_by_pointer(dPtr); // has effect on d
// print out results
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
}
[https://code.sololearn.com/cHCZ7DYfVrTG]