Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Scope of Variables Using Call-by-Reference

#include<iostream>
using namespace std;

void foo( int &score ); // & indicates a reference parameter

int main()
{
   int score = 5;

   cout << "\nIn main, score = " << score << endl;

   //call foo using call-by-reference
   foo( score );

   cout << "\nIn main, score = " << score << endl;

   return 0;

}

void foo( int &score )  // score is a reference parameter
{
  cout << "\nIn foo, score = " << score << endl;

  score = 10;

  cout << "\nIn foo, score = " << score << endl;

  return;

}
% g++ call_ref.cpp
% a.out

In main, score = 5

In foo, score = 5

In foo, score = 10

In main, score = 10

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming