You are on page 1of 11

Lab # 02: Functions II Page 1 of 11

CL103-Computer Programming
Lab # 02: Functions II
Objectives:

What are the Functions


Function Overloading
Functions with Default Parameters
Recursion
Inline Function

Instructions:

1. You are allowed to use your own laptops.


2. You can consult the books, manuals and class lectures.
3. You should have stationary like register and ballpoint to analyze the tasks first.
4. Ensure that your environment is working properly.
5. In practice session, consultation is allowed.
6. No discussion is allowed during tasks solution.
7. Attempt all questions yourself.
8. Submission of all tasks solutions is necessary in any way.
9. Tasks completed in specified time will be graded.
10. Cheating is strictly prohibited otherwise rules as per university will be applied as a result
of F-Grade in lab.

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 2 of 11

Getting Started

What are the Functions


Functions are like building blocks. They let you divide complicated programs into manageable pieces.
They have other advantages, too:

1. While working on one function, you can focus on just that part of the program and construct it,
debug it, and perfect it.
2. Different people can work on different functions simultaneously.
3. If a function is needed in more than one place in a program or in different programs, you can
write it once and use it many times.
4. Using functions greatly enhances the programs readability because it reduces the complexity of
the function main

Figure 1: Functions

#include <iostream>
#include <cctype>
#include <iomanip>

using namespace std;

double getNumber();
char identifyInput();
double getKilos (double);
double getMeters (double);

int main()
{
double numberIn = 0.0, kilos = 0.0, meters = 0.0, answer = 0.0 ;

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 3 of 11

char choice;
cout << showpoint << fixed << setprecision(2);

numberIn = getNumber();
choice = identifyInput();

if (choice =='P')
{
answer = getKilos (numberIn);
cout << numberIn << " pounds are equivalent to " << answer << " Kilograms.\n";
}
else
{
answer = getMeters (numberIn);
cout << numberIn << " yards are equivalent to " << answer << " Meters.\n";
}
system("pause");
return 0;
}

double getNumber()
{
double input;
do
{
cout << "Enter a number greater than 0 that represents a\n"
<< "measurement in either pounds or yards: ";
cin >> input;
}while (input <= 0);
return input;
}

char identifyInput()
{
char option;
do
{
cout << "Enter a P if the number you input was pounds and Y if the\n"
<< "number input was yards: ";
cin >> option;
option = toupper(option);
}while(!(option == 'P' || option == 'Y'));
return option;
}

double getKilos (double lbs)


{
double result;
result = lbs * 0.45359237;
return result;
}

double getMeters ( double yds)


{
double result;
result = yds * 0.9144;
return result;

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 4 of 11

}
/* Sample Run-I:
Enter a number greater than 0 that represents a
measurement in either pounds or yards: 512
Enter a P if the number you input was pounds and Y if the
number input was yards: P
512.00 pounds are equivalent to 232.24 Kilograms.

Sample Run-II:
Enter a number greater than 0 that represents a
measurement in either pounds or yards: 512
Enter a P if the number you input was pounds and Y if the
number input was yards: Y
512.00 yards are equivalent to 468.17 Meters.

Press any key to continue . . .


*/

Function Overloading
Creating several functions with the same name but different formal parameters.
Two functions are said to have different formal parameter lists if both functions have:

A different number of formal parameters or


If the number of formal parameters is the same, then the data type of the formal parameters, in
the order you list them, must differ in at least one position.

If a functions name is overloaded, then all of the functions in the set have the same name. Therefore, all
of the functions in the set have different signatures if they have different formal parameter lists. Thus,
the following function headings correctly overload the function functionXYZ:

void functionXYZ()
void functionXYZ(int x, double y)
void functionXYZ(double one, int y)
void functionXYZ(int x, double y, char ch)

Consider the following function headings to overload the function functionABC:

void functionABC(int x, double y)


int functionABC(int x, double y)

Both of these function headings have the same name and same formal parameter list. Therefore, these
function headings to overload the function functionABC are incorrect. In this case, the compiler will
generate a syntax error.

#include <iostream>
#include <conio.h>

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 5 of 11

using namespace std;

void repchar();
void repchar(char);
void repchar(char, int);

void main()
{
repchar();
repchar('=');
repchar('+', 30);

_getch();
}

void repchar()
{
for(int j=0; j<45; j++)
cout << '*';

cout << endl;


}

void repchar(char ch)


{
for(int j=0; j<45; j++)
cout << ch;

cout << endl;


}

void repchar(char ch, int n)


{
for(int j=0; j<n; j++)
cout << ch;

cout << endl;


}

Output
*********************************************
=============================================
++++++++++++++++++++++++++++++

Functions with Default Parameters


#include <iostream>
#include <conio.h>

using namespace std;

void repchar(char='*', int=45);

int main()

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 6 of 11

{
repchar();
repchar('=');
repchar('+', 30);

_getch();
}

void repchar(char ch, int n)


{
for(int j=0; j<n; j++)
cout << ch;

cout << endl;


}

Output
*********************************************
=============================================
++++++++++++++++++++++++++++++

This example further illustrates functions with default parameters.


#include <iostream>
#include <iomanip>
using namespace std;
int volume(int l = 1, int w = 1, int h = 1);
void funcOne(int& x, double y = 12.34, char z = 'B');
int main()
{
int a = 23;
double b = 48.78;
char ch = 'M';
cout << fixed << showpoint;
cout << setprecision(2);
cout << "Line 1: a = " << a << ", b = "
<< b << ", ch = " << ch << endl; //Line 1
cout << "Line 2: Volume = " << volume()
<< endl; //Line 2
cout << "Line 3: Volume = " << volume(5, 4)
<< endl; //Line 3
cout << "Line 4: Volume = " << volume(34)
<< endl; //Line 4
cout << "Line 5: Volume = "
<< volume(6, 4, 5) << endl; //Line 5
funcOne(a); //Line 6
funcOne(a, 42.68); //Line 7
funcOne(a, 34.65, 'Q'); //Line 8
cout << "Line 9: a = " << a << ", b = "
<< b << ", ch = " << ch << endl; //Line 9
return 0;
}
int volume(int l, int w, int h)
{
return l * w * h; //Line 10
}

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 7 of 11

void funcOne(int& x, double y, char z)


{
x = 2 * x; //Line 11
cout << "Line 12: x = " << x << ", y = "
<< y << ", z = " << z << endl; //Line 12
}
/*Sample Output
Line 1: a = 23, b = 48.78, ch = M
Line 2: Volume = 1
Line 3: Volume = 20
Line 4: Volume = 34
Line 5: Volume = 120
Line 12: x = 46, y = 12.34, z = B
Line 12: x = 92, y = 42.68, z = B
Line 12: x = 184, y = 34.65, z = Q
Line 9: a = 184, b = 48.78, ch = M
Press any key to continue . . .
*/

#include <iostream>
#include <iomanip>

using namespace std;

double computeAid(double costs, double gradePoint = 2.0);

int main()
{
double tuition, gpa, aid;

cout << "Enter the amount of your tuition costs not to exceed $2,000.00: ";
cin >> tuition;
while(tuition > 2000)
{
cout << "Your tuition may not exceed $2.000.00 Please enter it again: ";
cin >> tuition;
}

cout << "Enter your GPA (a value between 0 and 4.0): ";
cin >> gpa;
while (gpa < 0.0 || gpa > 4.0)
{
cout << "Please enter a GPA that is between 0 and 4.0: ";
cin >> gpa;
}

if (gpa < 2.0)


{
cout << "Sorry, You do not qualify academically for financial aid.\n";
system("pause");
return 0;
}

if (gpa > 3.0)


aid = computeAid (tuition, gpa);
else

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 8 of 11

aid = computeAid (tuition);

cout << "You are entitled to receive $" << aid << " in financial aid.\n";

system("pause");
return 0;
}

double computeAid(double costs, double gradePoint)


{
double amount = 0.0;
amount = costs / 4 * gradePoint;
return amount;
}
/*Sample Output
Enter the amount of your tuition costs not to exceed $2,000.00: 1200
Enter your GPA (a value between 0 and 4.0): 3.66
You are entitled to receive $1098 in financial aid.
Press any key to continue . . .
*/

Recursion
Recursion involves a function calling itself.

Factorial using Recursion


#include <iostream>
#include <conio.h>

using namespace std;

long factfunc(int n);

void main()
{
int n;
long fact;

cout <<"Enter an integer: ";


cin >> n;

fact = factfunc(n);

cout << "Factorial of " << n << " is " << fact << endl;

_getch();
}

long factfunc(int n)
{
if(n > 1)
return n * factfunc(n-1); //self call
else
return 1;

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 9 of 11

Sample Output
Enter an integer: 3
Factorial of 8 is 6

Figure 2: Factorial of 3

Figure 3: Factorial of 3

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 10 of 11

Figure 4: Factorial of 5

Inline Function

Figure 5: Inline Functions

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman


Lab # 02: Functions II Page 11 of 11

#include <iostream>
#include <conio.h>

using namespace std;

inline float lbstokg(float pounds);

void main()
{
float lbs;

cout << "Enter your weight in pounds: ";


cin >> lbs;
cout << "Your weight in kilograms is "<< lbstokg(lbs)<< endl;

_getch();
}

inline float lbstokg(float pounds)


{
return 0.453592 * pounds;
}

Sample Output
Enter your weight in pounds: 139
Your weight in kilograms

CL103-Computer Programming Compiled By: Mr. Najeeb Ur Rehman

You might also like