Scope Example


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

Error   : undefined identifier 'Local_to_main_too'
ScopeExample.cpp line 42   cout << "Local_to_main_too = " << Local_to_main_too << endl;

Error   : undefined identifier 'Local_to_braces'
ScopeExample.cpp line 43   cout << "Local_to_braces = " << Local_to_braces << endl;

Error   : undefined identifier 'Function_parameter'
ScopeExample.cpp line 44   cout << "Function_parameter = " << Function_parameter << endl;

Error   : undefined identifier 'Local_to_function'
ScopeExample.cpp line 45   cout << "Local_to_function = " << Local_to_function << endl;

Error   : undefined identifier 'Local_to_if'
ScopeExample.cpp line 61   cout << "Local_to_if = " << Local_to_if << endl;

Error   : undefined identifier 'Function_parameter'
ScopeExample.cpp line 64   cout << "Function_parameter = " << Function_parameter << endl;

Error   : undefined identifier 'Local_to_function'
ScopeExample.cpp line 65   cout << "Local_to_function = " << Local_to_function << endl;

Error   : undefined identifier 'Local_to_if'
ScopeExample.cpp line 73   cout << "Local_to_if = " << Local_to_if << endl;

Error   : undefined identifier 'Local_to_braces'
ScopeExample.cpp line 75   cout << "Local_to_braces = " << Local_to_braces << endl;

Error   : undefined identifier 'Function_parameter'
ScopeExample.cpp line 76   cout << "Function_parameter = " << Function_parameter << endl;

Error   : undefined identifier 'Local_to_function'
ScopeExample.cpp line 77   cout << "Local_to_function = " << Local_to_function << endl;

Error   : undefined identifier 'Local_to_main'
ScopeExample.cpp line 100   cout << "Local_to_main = " << Local_to_main << endl;

Error   : undefined identifier 'Local_to_if'
ScopeExample.cpp line 101   cout << "Local_to_if = " << Local_to_if << endl;

Error   : undefined identifier 'Local_to_main_too'
ScopeExample.cpp line 102   cout << "Local_to_main_too = " << Local_to_main_too << endl;

Error   : undefined identifier 'Local_to_braces'
ScopeExample.cpp line 103   cout << "Local_to_braces = " << Local_to_braces << endl;



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Oct 5 15:12:59 1999 .

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

Return to Coms 261 home page