Escolar Documentos
Profissional Documentos
Cultura Documentos
(CL214)
LABORATORY MANUAL
Spring 2017
LAB 05
Functions
Engr. Maryam Wasim
Engr. Moomal Bukhari
________________________________________ __________ ___
STUDENT NAME ROLL NO SEC
______________________________________
LAB ENGINEER SIGNATURE & DATE
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.
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.
3. Structure of a Function
The declaration syntax of a function is as follows:
Spring 2017:
NUCES, ISLAMABAD Page 2 of 7
Programming Fundamentals Lab
Functions LAB 05
# include <iostream>
using namespace std;
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)
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
c = xxx x=3 c = 15
y=5
z = 15
# include <iostream>
using namespace std;
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)
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
c = xxx x=7 c = 35
y=5
z = 35
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
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
Spring 2017:
NUCES, ISLAMABAD Page 7 of 7
Programming Fundamentals Lab