Function Worksheet II

Show what will be printed by each of the following programs.


1.  #include <iostream.h>
    void doglobal();
    void dolocal();
    void doref(int&);
    void doval(int);
    int x;
    int main()
    {
       x = 15;
       doref(x);
       cout << "x = " << x << " after the call to doref\n";
       x = 16;
       doval(x);
       cout << "x = " << x << " after the call to doval\n";
       x = 17;
       dolocal();
       cout << "x = " << x << " after the call to dolocal\n";
       x = 18;
       doglobal();
       cout << "x = " << x << " after the call to doglobal\n";
       return 0;
    }
    void doref(int& a)
    {
       a = 3;
    }
    void doval(int b)
    {
       b = 4;
    }
    void dolocal()
    {
       int x;
       x = 5;
    }
    void doglobal()
    {
       x = 7;
    }


2.  #include <iostream.h>
    int num = 10;
    void one();
    void two(int);
    void three();
    void four(int&);
    void five(int&);
    int main()
    {
       int num = 1;
       cout << "At start of main num = " << num << endl;
       one();
       cout << "After call to one num = " << num << endl;
       two(num);
       cout << "After call to two num = " << num << endl;
       three();
       cout << "After call to three num = " << num << endl;
       four(num);
       cout << "After call to four num = " << num << endl;
       two(num);
       cout << "After call to two num = " << num << endl;
       one();
       cout << "After call to one num = " << num << endl;
       five(num);
       cout << "After call to five num = " << num << endl;
       one();
       cout << "After call to one num  " << num << endl;
    }
    void one()
    {
       cout << "   At the start of one num = " << num << endl;
       num = 50;
       cout << "   At the end of one num = " << num << endl;
    }
    void two(int num)
    {
       cout << "   At the start of two num = " << num << endl;
       num = 5;
       cout << "   At the end of two num = " << num << endl;
    }
    void three()
    {
       int num = 100;
       cout << "   At the start of three num = " << num << endl;
       num = 200;
       cout << "   At the end of three num = " << num << endl;
    }
    void four(int& num)
    {
       cout << "   At the start of four num = " << num << endl;
       num = 25;
       cout << "   At the end of four num = " << num << endl;
    }
    void five(int& i)
    {
       cout << "   At the start of five num = " << num << endl;
       num = 2;
       i = 3;
       cout << "   At the end of five num = " << num << endl;
    }


3.  #include <iostream.h>
    void triple(int);
    int main(void)
    {
       int x;
       for (x = 1; x <= 5; x++)
          triple(x);
    }

    void triple(int value)
    {
       static int total = 0;
       int answer;
       answer = 3 * value;
       total += answer;
       cout << value << ' ' << answer << endl;
       cout << "total " << total
            << endl << endl;
    }



Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2002