You are on page 1of 14

LAB 01

Summary

Items Description

Course Title Object Oriented Programming

Lab Title Revision of Functions

Duration 3 Hours

Operating System Visual C++ / Visual Studio


/Tool/Language

CLO2 Report the outcome of an experiment / task in standard format

Objectives

You have already seen C++ functions in your first course on Programming. This lab activity is
intended to provide an overview/recap of:

 C++ Functions and their usage


 Function definition, function declaration and function call
 The difference between call by value and call by reference
 Overloaded functions and functions with default parameters
 Recursive and inline functions

Example 1.1 Practice

1|Page
Scope of Variables

Example 1.2 Practice

// function example #include <iostream> The first result is 5


using namespace std; The second result is 5
The third result is 2
int subtraction (int a, int b) The fourth result is 6
{ int r; r=a-b; return r;
}

int main ()
{
int x=5, y=3, z; z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) <<
'\n';
cout << "The third result is " << subtraction (x,y) <<
'\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n'; return 0;

Functions with no type – The use of void

2|Page
Example 1.3 Practice

// void function example #include


I'm a function!
<iostream>
using namespace std;

void printmessage ()
{
cout << "I'm a function!";
}

int main ()
{
printmessage ();
return 0;
}

Arguments passed by Value and by Reference

Example 1.4

x=2, y=6, z=14

3|Page
// passing parameters by reference
#include <iostream>
using namespace std;

void duplicate (int& a, int& b, int& c)


{
a=a*2; b=b*2;
c=c*2;
}
int main ()
{
int x=1, y=3, z=7; duplicate (x,
y, z);
cout << "x=" << x << ", y=" << y << ", z="
<< z; return
0;
}

Example 1.5

// more than one returning value


Previous=99, Next=101
#include <iostream> using
namespace std;
void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}
int main ()
{
int x=100, y, z; prevnext
(x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}

Default values in Parameters

Example 1.6 Practice

6
5

4|Page
// default values in functions #include
<iostream>
using namespace std;

int divide (int a, int b=2)


{ int r;
r=a/b;
return r;
}

int main ()
{
cout << divide (12); cout
<< endl;

cout << divide (20,4);


return 0;
}

Overloaded Functions

Example 1.7 Practice

// overloaded function #include10 2.5


<iostream>
using namespace std;

int operate (int a, int b)


{
return a*b;
}

float operate (float a, float b)


{
return a/b;
}

5|Page
int main ()
{
int x=5,y=2; float
n=5.0,m=2.0; cout <<
operate (x,y); cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}

Declaring Function Prototypes

Example 1.8

// factorial calculator #include Please type a number: 9 9! =


<iostream> 362880
using namespace std;

long factorial (long a)


{
if (a > 1)
return (a * factorial (a-1)); else
return (1);
}

int main ()
{
long number;
cout << "Please type a number: "; cin >>
number;
cout << number << "! = " << factorial (number);

return 0;
}

6|Page
Inline Functions

Example 1.9

// TestInlineFunction.cpp
#include <iostream> using
namespace std;

inline int max(int n1, int n2)


{ return (n1 > n2) ? n1 : n2;
}

int main() { int i1 = 5, i2 =


6; cout << max(i1, i2) << endl;
return 0;
}

LAB TASKS
Task 1 :
Give answers to the following.

7|Page
1. Write the prototype of a function named fnct() which accepts an int by value, a float by
reference and returns a char.

char fnct(int num, float *float)


2. Using three variables a,b & c, call the function: double power(double, double);

3. Which of these are valid function declarations:

a. void function();
b. void function(void);
c. void function(int);
d. function(int);
e. int function();

function(int) : This declaration is not valid because it has not the return type. A return
type is necessary for all functions in C++.

4. A function needs to compute the average as well as the sum of three integers passed to it
and return the answers in the main(). Suggest the prototype for this function.

void computeAverage And Sum(int a, int b, int c, float &average, int &sum);

5. What are inline functions?

Inline functions in C++ are like shortcuts. Instead of going through the process of
calling a function, the code inside the function is copied right where it's needed. It's like

8|Page
writing the same thing over and over again instead of making a phone call. This can
make things faster because it skips the extra steps of making that call.

Task 2 :

Write the output of the following code fragments.


1. int cube(int); int
main()
{

for(int i=0;i<10;i+=3)
cout << cube(i) << endl;

return 0;
}
int cube(int a)
{

return a*a*a;
}
Output:
0
27
216
729

2. int larger(int,int); int main()


{
int x=10,y=5; int m =
larger(x,y); cout<<m<<endl;
return 0;
}
int larger(int a,int b)
{
if (a>b)
return a; else
return b;
}

9|Page
Output:
10

3. void decrement(int); int main() {

int x=10; cout<< x <<endl;


decrement(x); cout<< x <<endl;
return 0;
}
void decrement(int x)
{
x--;
cout<< x <<endl;
x--;

Output:
10
9
8

Task # 03

COMPILE a program with a function isPrime() which takes an integer as an argument and returns
true if the argument is a prime number.

10 | P a g e
Task # 04

Write a program with a function volume() which accepts three sides of a cube and returns its volume.
Provide a default value of 1 for all the three sides of a cube.
Call this function with zero, one, two and three arguments and display the volume returned in the
main().

Practice From Website on dev c++ W3 schools

1 . Functions

11 | P a g e
2

Parameters and Arguments


3

12 | P a g e
5

7 8

13 | P a g e
9 10

14 | P a g e

You might also like