// this program illustrates how call by reference works. the value of // a formal parm is changed inside the function and the change is passed // back to the caller because the parameter is passed by reference. the // & in the type of the formal parm is what makes it a reference parm #include <iostream.h> void sample(int&); int main() { int num; num = 5; cout << "Before the call num = " << num << endl; sample(num); cout << "After the call num = " << num << endl; return 0; } // sample changes the value of num and prints it // the change is returned to the caller since num is passed by reference void sample(int& num) { num = 10; cout << "Inside the function num is changed to " << num << endl; }
Before the call num = 5 Inside the function num is changed to 10 After the call num = 10
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002