Scope Example, v.2


ScopeExample.cpp

Program listing

/************************************************************************
*                                                                       *
*   Scope Example program                                               *
*                                                                       *
************************************************************************/

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

void ScopeFunc(int);

//  Global variable

int Global = 111;

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
************************************************************************/

int main() {

//  Local to main

    int Local_to_main = 222;
    
    if (true) {
    
    //  Local to if block
    
        int Local_to_if = 333;
        
        cout << "Inside if:" << endl << endl;
        cout << "Global = " << Global << endl;
        cout << "Local_to_Main = " << Local_to_main << endl;
        cout << "Local_to_if = " << Local_to_if << endl;
//      cout << "Local_to_main_too = " << Local_to_main_too << endl;
//      cout << "Local_to_braces = " << Local_to_braces << endl;
//      cout << "Function_parameter = " << Function_parameter << endl;
//      cout << "Local_to_function = " << Local_to_function << endl;
    }

//  Another local to main

    int Local_to_main_too = 444;
    
    {
    
    //  Local to this block
    
        int Local_to_braces = 555;
        
        cout << endl << "Inside braces:" << endl << endl;
        cout << "Global = " << Global << endl;
        cout << "Local_to_main = " << Local_to_main << endl;
//      cout << "Local_to_if = " << Local_to_if << endl;
        cout << "Local_to_main_too = " << Local_to_main_too << endl;
        cout << "Local_to_braces = " << Local_to_braces << endl;
//      cout << "Function_parameter = " << Function_parameter << endl;
//      cout << "Local_to_function = " << Local_to_function << endl;
    }

//  Which ones can we access in main?

    cout << endl << "Inside main:" << endl << endl;
    cout << "Global = " << Global << endl;
    cout << "Local_to_main = " << Local_to_main << endl;
//  cout << "Local_to_if = " << Local_to_if << endl;
    cout << "Local_to_main_too = " << Local_to_main_too << endl;
//  cout << "Local_to_braces = " << Local_to_braces << endl;
//  cout << "Function_parameter = " << Function_parameter << endl;
//  cout << "Local_to_function = " << Local_to_function << endl;

//  Now go to a function

    ScopeFunc(666);
    
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   ScopeFunc                                               *
*                                                                       *
************************************************************************/

void ScopeFunc(int Function_parameter) {
    
    int Local_to_function = 777;

//  Which ones can we access in the function?

    cout << endl << "Inside function:" << endl << endl;
    cout << "Global = " << Global << endl;
//  cout << "Local_to_main = " << Local_to_main << endl;
//  cout << "Local_to_if = " << Local_to_if << endl;
//  cout << "Local_to_main_too = " << Local_to_main_too << endl;
//  cout << "Local_to_braces = " << Local_to_braces << endl;
    cout << "Function_parameter = " << Function_parameter << endl;
    cout << "Local_to_function = " << Local_to_function << endl;
    return;
}

Sample Run

Inside if:

Global = 111
Local_to_Main = 222
Local_to_if = 333

Inside braces:

Global = 111
Local_to_main = 222
Local_to_main_too = 444
Local_to_braces = 555

Inside main:

Global = 111
Local_to_main = 222
Local_to_main_too = 444

Inside function:

Global = 111
Function_parameter = 666
Local_to_function = 777



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Oct 5 15:07:02 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/ScopeExample.html

Return to Coms 261 home page