0% found this document useful (0 votes)
96 views5 pages

Lab 8:more On Functions: Objectives

The document provides instructions for Lab 8 of a computer programming course, which focuses on variable scope and overloaded functions in C++. It includes examples of using different variable scopes like block, parameter, for-loop, and global scopes. It also demonstrates function overloading by defining multiple functions with the same name but different parameters. Students are asked to analyze code snippets involving these concepts and identify issues related to variable scoping. They are also tasked with writing overloaded functions to calculate the areas of different types of triangles.

Uploaded by

Wassen Hejjawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views5 pages

Lab 8:more On Functions: Objectives

The document provides instructions for Lab 8 of a computer programming course, which focuses on variable scope and overloaded functions in C++. It includes examples of using different variable scopes like block, parameter, for-loop, and global scopes. It also demonstrates function overloading by defining multiple functions with the same name but different parameters. Students are asked to analyze code snippets involving these concepts and identify issues related to variable scoping. They are also tasked with writing overloaded functions to calculate the areas of different types of triangles.

Uploaded by

Wassen Hejjawi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Qatar University

College of Engineering
Dept. of Computer Science & Engineering

Computer Programming
GENG106
Lab Handbook
Fall 2012 Semester

Lab 8 :More on Functions


Objectives
At the end of this lab, you should be able to
Use and practice the basic rules governing variables scope in C++
Practice writing and calling overloaded functions.

Quick Review

The Scope of a particular variable is the range within a program's source code in which that
variable is recognized by the compiler.
The basic rules governing the scope of C++ variables can be summarized as follows:
o Block scope: variables declared inside a block are recognized inside that block only.
o Parameter scope: variables declared as parameters to a function are only recognized
inside the function.
o for-loop scope: A variable declared in the initialization expression of a for loop is local
to just the for loop.
o Global scope: the scope of a variable declared outside any curly-brace blocks lasts from
the variable's declaration to the end of the file.
Generally you should define a variable when you first need it, but you have to pay attention to
the scope. A variable's scope determines when it is valid. So if we try to access a variable outside
its scope, the access is invalid, and the compiler will complain.

1) Find what is wrong with this function's variable scoping, and then fix it.
/* PURPOSE:
Select the maximum of three integer values
RECEIVES: int i, j, k
*/
int getMaximum(int i, int j, int k)
{
if (i > j)
{
int a;
a = i;
}
else
{
a = j;
}
if (k > a)
{
return k;
}
else
{
return a;
}
}

2) In the following program the variable x is declared 4 times. Study the program and then answer
the questions given below.
#include <iostream>
using namespace std;
int x=3;
void f1()
{
int x=4;
cout<<"value of x in f1="<<x<<endl;
}
void f2()
{
int x=5;
cout<<"value of x in f2="<<x<<endl;
}
int main()
{
cout<<"value of x in main="<<x<<endl;
f1();
f2();
for(int x=20;x<=100;x+=20)
cout<<"value of x in main(inside for)="<<x<<endl;
cout<<"value of x in main="<<x<<endl;
return 0;
}

Run the program, and then write down its output.


___________________________
___________________________
___________________________
___________________________
___________________________

If you delete declaration of x in function f1, what would the output be?
___________________________
___________________________
___________________________
___________________________
___________________________

Explain the change: ________________________________________________________


Now delete the declaration of x inside the for-loop.
What is the difference? Explain.
Add comments in front of each variable x to specify scope of this variable.

3) Eliminating Global Variables


Global variables may "work", but they can complicate the program and create confusion since all
functions can set a global variable. It is often difficult to find the error if the global variable is set
to the wrong value.
int maximum; // maximum is a global variable.
// Updates maximum if parameter is larger
void set_max(int a)
{
if (maximum < a)
maximum = a;
}
int max3(int i, int j, int k)
{
maximum = i;
set_max(j);
set_max(k);
return maximum;
}
int main()
{
int i, j, k;
cout << "Enter first integer : "; cin >> i;
cout << "Enter second integer: "; cin >> j;
cout << "Enter third integer : "; cin >> k;
maximum = max3(i, j, k);
cout << "The maximum = " << maximum << endl;
return 0;
}

1. Encircle the word maximum in the above code. Can you observe the effect of the global
declaration. All blocks can identify this variable.
2. Remove the int maximum; from the top of the code and place it inside the main()
function.
3. If you compile the code, both max3()and set_max()manifest errors because maximum;
is not identified by them.
4. Re-write max3() and set_max() functions (avoid the use of global variables) while
preserving the logic of the complete code.

4) Function Overloading
Function overloading means that two or more functions share the same name but their signatures
are different. A function's signature refers to the number and type of its parameters.
Run the following program
#include <iostream>
using namespace std;
void print(int i){
cout << Here is int << i << endl;
}
void print(double f) {
cout << Here is double << f << endl;
}
void print(char c) {
cout << Here is char << c << endl;
}
int main() {
print(10);
print(10.15);
print(t);
return 0;
}

Write down the output of the program


___________________________
___________________________
___________________________

Can we add another function called print that takes parameter of type int but returns int
(difference based on return type)
Yes [ ]

No [ ]

5) Consider the following examples of overloaded functions. How do each of the following differ in
signature? (Indicate "same" or "different" in the following table)
variable scopes and overloaded functions

Function Prototype

# of param's
add(int i, int j);
add(int i, double j);
add(int i, int j);
add(int i, int j, int k);
add(int i, int j, int k);
add(double i, double j, double k);
add(double i, double j, double k);
add(int i, double j, int k);

Implement and test the following overloaded functions:


void add(int i, int j);
void add(double i, double j);

Type of param's

6) Function Overloading:
a. Write and test a C++ function (getArea()) that has three double parameters: a, b,
and c that represent the sides of a triangle. Let your function return -1 if the three
parameters do not represent a triangle, and return the area of the triangle otherwise.
Hint: A simple test for that a, b and c represent a triangle is by checking that the sum of any
two numbers is greater than the third.

To calculate the area, use Heron's formula


If
triangle has sides a, b, c

and

abc
Semiperimeter
2

then
Area of triangle ABC:

s(s a )(s b)(s c)

b. Add an overloaded function that returns the area of a right-angled triangle.


Hint: getArea() takes only two parameters, the base and height.
c. Add an overloaded function that returns the area of an equilateral triangle.
Hint: getArea() takes only one parameter, side.

Scalene triangle

Equilateral triangle

Right-angled triangle

You might also like