CS
010 - Introduction to Computer Science I
Lab 4. Scope and Functions, Using the Debugger
Parts 1, 2, 3,
4, 5, 6,
7 should be completed in the lab period
Parts 8 and 9 are challenge exercises
(worth a small bonus!) to be attempted in whatever remaining time you have.
Grading
Attendance & participation: 2 points
Parts 1 : 2 points
Parts 2, 3, 4, 5, 6, & 7: 1 point each
Parts 8 & 9: (bonus) 0.25 points each
After this lab you should be:
Familiar with the scope rules
Understand how to write function definitions & function calls
Familiar with the debugger
Notes
An identifier is a name: a variable name, a function name, a data-type name, a reference
alias, 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, function definitions, and function prototypes 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 std::cout;
using std::endl;
void a( void );
void b( void );
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);
}
void a( void )
{
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;
}
void b( void )
{
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;
}
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:
Familiarize yourself with the debugger on a simple program.
(The site http://www.cse.fau.edu/~cot3002l/vcc-debug/
is useful for this)
Part 7:
Run the program from part 1 - 5 through the debugger, tracing the value of
one of your variables through the course of the program.
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?
void A();
int main()
{
cout << "hello" << endl;
A();
}
#include <iostream>
void A()
{
cout << "world" << endl;
}
Part 9:
#include <iostream>
using namespace std;
int x=5;
int main()
{
int x=10;
//add a line here, which prints out the value of the global variable x
}