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

 
Global Variable Example

// Global Variable Example
#include<iostream>
using namespace std;

int globe = 99; // global variable

void foo( int score);

int main()
{
   int score = 5;  // local variable

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

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

   foo( score );

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

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

   return 0;

}

void foo( int score )
{
  // score is local
 
  cout << "\nIn foo, score = " << score << endl;

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

  score = 10;

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

  globe = 222;

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

  return;

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

In main, score = 5

In main, globe = 99

In foo, score = 5

In foo, globe = 99

In foo, score = 10

In foo, globe = 222

In main, score = 5

In main, globe = 222


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