You are on page 1of 7

Programming Fundamentals

(CL214)
LABORATORY MANUAL
Spring 2017

LAB 05
Functions
Engr. Maryam Wasim
Engr. Moomal Bukhari
________________________________________ __________ ___
STUDENT NAME ROLL NO SEC

______________________________________
LAB ENGINEER SIGNATURE & DATE

MARKS AWARDED: /10


NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD

Last Edited by: Engr. Moomal Bukhari Version: 3.01


Prepared by: Engr. Moomal Bukhari Date: 9 Feb, 2017
Verified by: Engr. Azhar Rauf Date: 10 Feb, 2017
Functions LAB 05

LAB 05 Functions

Lab Objectives:
1. To understand the advantages of using functions
2. To learn the structure and syntax of a function
3. To learn passing arguments by-value and by-reference

Software Required:
Visual Studio 2010

Introduction:
1. Functions
Functions are like subtasks. They receive some information, do some process and
provide a result. Functions are invoked through a calling program. Calling program does
not need to know what function is doing and how it is performing its task. There is a
specific function-calling methodology. The calling program calls a function by giving it
some information and receives the result.
We have a main( ) in every C++ program. main( ) is also a function. When we write a
function, it must start with a name, parentheses, and surrounding braces just like with
main( ). Functions are very important in code reusing.

There are two categories of functions:


1. Functions that return a value
2. Functions that do not return a value

Suppose, we have a function that calculates the area of a rectangle such that function
will return the calculated area. Similarly we may have a function which displays some
information on the screen so this function is not supposed to return any value to the
calling program.

2. Advantages of Using a Function


1. Code Reusability
2. Modularity
3. Readability

3. Structure of a Function
The declaration syntax of a function is as follows:

return-value-type function-name ( argument-list )


{
declarations and statements
}

Spring 2017:
NUCES, ISLAMABAD Page 2 of 7
Programming Fundamentals Lab
Functions LAB 05

# include <iostream>
using namespace std;

int Area (int , int ); // Function prototype


int main ()
{
int a, b, c;
a = 3;
b = 5;
c = Area (a, b); // Function call
cout << "The Area of " << a << "X" << b << " rectangle is: " << c << endl;

return 0;
}

// Function definition
int Area (int x, int y)
{
int z;
z = x * y;
return z;
}
Code 1. Calculating the Area of a Rectangle (Pass by Value)

4. Memory Map of a Normal Function Call (Pass by Value)


Consider the Code 1 above. Before function call, the memory will contain three variable
a, b and c. Inside function, three additional variables, x, y and z, will be created. Values
of a and b will be copied into x and y as these are function arguments. After function call
ends, value of z, which is 15, will be copied onto c. Variables x, y and z will be destroyed
or deallocated after function call. Figure 1 gives a picture of exactly how this happens.
int Area (int x, int y)

main ()
int z;
2 z = x * y;
1 int a, b, c ;
a = 3; return z;
b = 5;
c = Area (a, b);
3
return 0;

Memory Before 3

Memory Before 2 a=3 Memory After 3

a=3 b=5 a=3

b=5 c = xxx b=5

c = xxx x=3 c = 15

y=5

z = 15

Figure 1. A Normal Function Call


Spring 2017:
NUCES, ISLAMABAD Page 3 of 7
Programming Fundamentals Lab
Functions LAB 05

5. Memory Map of a Referenced Function Call (Pass By Reference)


If function arguments are passed by reference then function arguments directly refer to
the original variables. New variables are not created. If the value of function arguments
is changed then the value of original arguments is also varied. Syntax for passing
variables by reference is given in Code 2. A diagrammatic view of how this happens is
given in Figure 2.

# include <iostream>
using namespace std;

int Area (int& , int& ); // Function prototype

int main (){


int a, b, c;
a = 3;
b = 5;
c = Area (a, b); // Function call
cout << "The Area of " << a << "X" << b << " rectangle is: " << c << endl;

return 0;
}
// Function definition
int Area (int& x, int& y){
int z;
x = 7;
z = x * y;
return z;
}
Code 2. Calculating the Area of a Rectangle (Pass by Reference)

int Area (int& x, int& y)

main () int z;
2 x = 7;
z = x * y;
1 int a, b, c ;
a = 3; return z;
b = 5;
c = Area (a, b);
3
return 0;

Memory Before 3

Memory Before 2 a=7 Memory After 3

a=3 b=5 a=7

b=5 c = xxx b=5

c = xxx x=7 c = 35

y=5

z = 35

Figure 2. A Referenced Function Call


Spring 2017:
NUCES, ISLAMABAD Page 4 of 7
Programming Fundamentals Lab
Functions LAB 05

Practice Problems:

1. You are given two programs on a separate handouts. For each program, you are
required to figure out what the program does and write its output by dry-run.

2. Write a function that calculates the power of an integer number. For example you want
to calculate (2)5. Your function takes 2 and 5 as arguments and returns the value 32.
Take the number and its power as input from the user.

3. Now rewrite the function in task 2 above by not explicitly returning the power of an
integer. Rather, the power should be calculated in the same location where the power
was stored in the main program (e.g. 5 in above task).

4. Write a program that takes an integer as input from the user. Pass that integer as an
argument to a function named as evenodd (int) that prints number is even or number is
odd after evaluating the number.

Spring 2017:
NUCES, ISLAMABAD Page 5 of 7
Programming Fundamentals Lab
Functions LAB 05

Submission Declaration by the Student:


In submitting this lab write-up to the Lab Engineer/Instructor, I hereby declare that:
I have performed all the practical work myself
I have noted down actual measurements in this write-up from my own working
I have written un-plagiarized answers to various questions
I have/have not obtained the desired objectives of the lab.
Reasons of not obtaining objectives (if applicable): _________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________
Students signature and Date

Student Evaluation by the Lab Engineer:


The Lab Engineer can separate this page from the write-up and keep it for his/her own record. It
must be signed by the student with date on it.
Lab Work: objectives achieved (correctness of measurements, calculations, answers to
questions posed, conclusion) ________/30
Lab Write-up: Neatness, appropriateness, in time submission ________/10
Troubleshooting: Were the students able to troubleshoot his/her work when it was purposely
changed? ________/10
TOTAL: ________/50

Feedback on student behavior:


Encircle your choice. -2 means poorest/worst/extremely inadequate/irrelevant, 0 gives an
average score, and +2 means best/most relevant/most adequate.

Did the student join the lab at the start/remained in lab? -2 -1 0 1 2


Did the student remain focused on his/her work during lab? -2 -1 0 1 2
Rate student's behavior with fellows/staff/Lab Engineer? -2 -1 0 1 2
Did the student cause any distraction during the Lab? -2 -1 0 1 2
Was the student found in any sort of plagiarism? -2 -1 0 1 2

Additional comments (if any) by the Lab Engineer:


__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
___________________________
Lab Engineers signature and Date

Spring 2017:
NUCES, ISLAMABAD Page 6 of 7
Programming Fundamentals Lab
Functions LAB 05

Student feedback: [Separate this page; fill it; drop in the Drop Box.]
Providing feedback for every lab session is optional. No feedback means you are satisfied.
The Lab Committee will consider only duly filled forms submitted within one week after the
lab.
This feedback is for lab session: Lab Number: _____, Date: _____________________
General (to provide feedback on a persistent practice/occurrence in labs).
Your current CGPA is in the range 4.00 to 3.00/2.99 to 2.00/1.99 to 1.00/0.99 to 0.00

Who conducted the lab? __________________________________________________


Actual start time: _______________ Total duration of lab: _______________________
Instruction duration: _________________ Practical duration: _____________________
Lab write-up available before lab? Yes/No with the Photocopier/in lab/on SLATE
Had the theory related to lab been covered in theory class? Yes/No
Encircle your choice. -2 means poorest/worst/extremely inadequate/irrelevant, 0 gives an
average score, and +2 means best/most relevant/most adequate.

Was duration of instruction session adequate? -2 -1 0 +1 +2


Instruction How much did you understand about the practical? -2 -1 0 +1 +2
Session How much content was irrelevant to the practical? -2 -1 0 +1 +2
Did the instructor allow Q/A and discussion? -2 -1 0 +1 +2
Practical Did you get sufficient time for practical? -2 -1 0 +1 +2
Presence in lab at all time? -2 -1 0 +1 +2
Ability to convey? -2 -1 0 +1 +2
Lab Readiness to help during practical? -2 -1 0 +1 +2
Engineer Readiness to discuss theoretical aspects? -2 -1 0 +1 +2
Helps in troubleshooting? -2 -1 0 +1 +2
Guides hows & whys of troubleshooting? -2 -1 0 +1 +2
How friendly was the lab staff? -2 -1 0 +1 +2
Staff Presence of staff throughout the lab session? -2 -1 0 +1 +2
Impact of availability of staff on your practical? -2 -1 0 +1 +2
Performance of electronic instruments? -2 -1 0 +1 +2
Equipment Performance of breadboard/experiment kit? -2 -1 0 +1 +2
Performance of circuit components esp. ICs? -2 -1 0 +1 +2
Overall Your overall rating for the whole lab session? -2 -1 0 +1 +2

Other comments: ____________________________________________________________


__________________________________________________________________________
__________________________________________________________________________

Spring 2017:
NUCES, ISLAMABAD Page 7 of 7
Programming Fundamentals Lab

You might also like