CS 010 - Introduction to Computer Science I
Lab 5. Scope and Functions

Points(10 overall)

Collaboration policy:

Collaboration on this lab exercise is strongly ENCOURAGED. The exercise is intended for practice, not assessment -- most people who try should get all participation points. Feel free to ask for help from, and provide help to, others. You shouldn't blindly copy solutions from one another (or from anywhere else) nor simply write code for someone else (even if you explain it as you write), but you can certainly help each other debug, give plenty of suggestions and hints, **explain** why things work or don't work, etc. If you get done early, feel free to walk around and help others.


Notes
An identifier is a name: a variable name, a function name, etc.

The portion of a program where an identifier has meaning is known as its scope. An identifier declared outside any function (i.e. in the header area) has file scope, and is known to all functions from the point at which it is defined to the end of the file. So Global variables and function declarations placed outside a function all have file scope.

When we define a variable inside a code block, it can be referred to only in that block, or in blocks nested within that block - it is said to be local to that block. So variables defined within a function have block scope, as do the parameters of the function; i.e. their scope is the function.

When an identifier in an outer block has the same name as an identifier in an inner (nested) block, the identifier in the outer block is "hidden" as long as the inner block is executing, even though it is still within scope: i.e. the program "sees" the value of the inner identifier while the inner code block is executing, and sees the value of the outer identifier while the outer code block is executing.


Part 1:
Read the following code and write down your predictions for the output values of the two distinct variables named x. Experiment with the code in Visual C++, and understand thoroughly why the output varies as it does.

#include <iostream>
using namespace std;

int a();
int b();

int x = 1;

int main()
{
    int x = 5;
    cout << "local x in outer scope of main is " << x << endl;
    {
         int x = 7;

         cout << "local x in inner scope of main is " << x << endl;

    }

    cout << "local x back in outer scope of main is " << x << endl;

    a();
    b();
    a();
    b();

    return(0);
}

int a()
{
    int x = 25;
    cout << endl << "local x at start of function a is " << x << endl;
    x++;
    cout << "local x at end of function a is " << x << endl;
    return(0);
}

int b()
{
    cout << endl << "global x at start of function b is " << x << endl;

    x *= 10;

    cout << "global x at end of function b is " << x << endl;
    return(0);
}



Part 2: Add a global variable y in the above program



Part 3: Add a function which prints out the values of the global variables x and y.



Part 4: Suppose global variables x and y stand for the opposite and adjacent sides of a right-angled triangle. Write a function which calculates the area of the triangle.



Part 5: Write a function, which calculates the hypotenuse of the right-angled triangle.



Part 6:

Do Programming Project #6 from Chapter 3 using CodeMate.

When you login to CodeMate, click on CodeMate Index on the left and choose Chapter 3. The programming projects are linked on the bottom.


Part 7:

Code Debugging:

The TA will give a short talk on methods to find logic errors in your programs.


Part 8:

If you remember, we always include the iostream header file prior to the main function. According to the scoping rule, the names declared in iostream can be accessed at any point between the #include directive and the end of the file. Can you find the error in the following program?

int A();
int main()
{
    cout << "hello" << endl;
    A();
}

#include <iostream>
int A()
{
    cout << "world" << endl;
    return(0);
}


Challenge Exercises:

Please feel free to help your fellow labmates reach this point!!

The following exercises are for your own benefit. You are not required to do these. In fact, they may contain material not covered in class or lab yet. As such, the TAs will give priority to helping students on the previous sections.


Challenge 1: