You are on page 1of 10

Function Overloading and Recursive Functions LAB 06

ProgrammingFundamentals
(CL214)
LABORATORY MANUAL
Spring 2017

LAB 06
Function Overloading and Recursion
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: 19 Feb, 2017
Verified by: Engr. Azhar Rauf Date: 20Feb, 2017

LAB06 Function Overloading and Recursive Functions

Lab Objectives:
1. To learn how to use overloaded functions.
2. To learn how to use recursive functions.

Software Required:
Visual Studio 2010
Spring 2017:
NUCES, ISLAMABAD Page 1 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

Introduction:
1. Function Overloading
Briefly, function overloading means two or more functions share the same name but their
parameters are different.
The functions that share the same name are said to be overloaded and the process is
called function overloading.
The number and types of a function's parameters are called the function's signature.
Together a function's name and its signature uniquely identify it.

//Overloaded functions with same number of parameters, but


//different types.
int timesTwo(int num);
double timesTwo(double num);

int main(void){
int A = 1;
double B = 1.1;

int x = timesTwo(A); // Changes A to 2


double y = timesTwo(B); // Changes B to 2.2
}

// handle int type


int timesTwo(int &num){
num = num * 2;
return num;
}

// handle double type


double timesTwo(double &num){
num = num * 2.0;
return num;
}
2. Recursive Function
A recursive function (DEF) is a function which either calls itself or is in a potential cycle
of function calls. The recursion continues until some condition is met. To prevent infinite
recursion, if...else statement (or similar approach) can be used where one branch makes the
recursive call and other doesn't (called base case).

A recursive function has the following general form (it is simply a specification of the general
function we have seen many times):

Spring 2017:
NUCES, ISLAMABAD Page 2 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

ReturnType Function( Pass appropriate arguments ) {

if a simple case, return the simple value //base case or stopping


condition (there may be several base cases)

else call function with simpler version of problem

For a recursive function to stop calling itself we require some type of stopping condition. If it
is not the base case, then we simplify our computation using the general formula.

// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}

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

2.1. Recursive vs non Recursive Functions


Recursion takesconsiderably longer time. This is because every time the recursive function
is called all the variables are copied for each pass and operating system is invoked for
assigning memory space.

Following are examples of a non-recursive implementation and a recursive implementation


for Fibonacci series. Run both the examples with n=45 for good demo of different execution

Spring 2017:
NUCES, ISLAMABAD Page 3 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

times.

Non-Recursive Implementation of Fibonacci series:


#include <iostream>
using namespace std;

/*non recursive fibonacci function*/


double fibonacci(int n)
{
double prev = -1;
double result = 1;
double sum;
int i;

for(i = 0;i <= n;++ i){


#include<iostream>sum = result + prev;
using namespace std;prev = result;
result = sum;
/*recursive}fibonacci function*/
double fibonacci(int n)
{ return result;
}
if((n==1)||(n==0))
{
int main(){
return(n);
} int n, i=0;
elsecout<<"Input the number of terms for Fibonacci Series:";
{ cin>>n;
cout<<"\nFibonacci Series is as follows\n";
return(fibonacci(n-1)+fibonacci(n-2));
}
} while(i<n){
cout<<" "<<fibonacci(i);
i++;
int main()
{ }
int return
n, i=0; 0;
}
cout<<"Input the number of terms for Fibonacci Series:";
Recursive
cin>>n; Implementation of Fibonacci series:
cout<<"\nFibonacci Series is as follows\n";

while(i<n)
{
cout<<" "<<fibonacci(i);
i++;
Spring 2017:
} NUCES, ISLAMABAD Page 4 of 10
Programming Fundamentals Lab

return 0;
}
Function Overloading and Recursive Functions LAB 06

Practice Problems:

1. Write the following functions using recursion :

a. A function which tells if the number is prime. The function should return bool type.

Spring 2017:
NUCES, ISLAMABAD Page 5 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

3. #include <iostream>
4. using namespace std;
5. bool prime(int x, int d);
6. int main()
7. {
8. int a;
9. cout << "enter number you want to check = ";
10. cin >> a;
11. if (prime(a, 2) == true)
12. {
13. cout << a << " is prime no " << endl;
14.
}
15. else {
16. cout << a << " is not prime no " << endl;
17. }
18.
19. }
20. bool prime(int x,int d )
21. {
22. if (d >= x)
23. return true;
24. if (x%d==0)

{
return false;
}
prime(x, d + 1);

a. A function to print numbers starting from zero counting up to the provided number.

#include <iostream>
using namespace std;
void n(int x, int l)
{

if (x >= l)
cout << l << endl;
else{
n(x, l + 1);
}

void main()
{
int a,b=0;

Spring 2017:
NUCES, ISLAMABAD Page 6 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

cout << "enter = ";


cin >> a;
n(a,b);
//cout << n(a) << endl;
}

2. Write a program that determines the smaller of the two integer numbers and the smaller of the
two strings (using ASCII values). Take the numbers and strings as input from the user. Use the
following overloaded function prototypes:
void smallest (int, int);
void smallest (string, string);

#include<iostream>
#include <string>

using namespace std;

void smallest(int a, int b)


{
int min;
if (a > b)
{
min = b;
cout << "THE SMALLER number is " << min << endl;
}
else
{
min = a;
cout << "the smaller number is " << min << endl;
}
}

void smallest(string q, string w)

{
int min;
int a = q.length();
int b = w.length();
if (a > b)
{
min = b;
cout << "the smallest string is " <<b << endl;
}
else
{
min = a;
cout << "the smallest string is " << a << endl;
}

Spring 2017:
NUCES, ISLAMABAD Page 7 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

int main()
{
int x, y;
string t, h;

cout << "enter number 1 " << endl;


cin >> x;

cout << "enter number 2 " << endl;


cin >> y;

cout << "enter string 1 " << endl;


cin>>t;

cout << "enter string 2 " << endl;


cin>> h;

smallest(x, y);
smallest(t, h);

Spring 2017:
NUCES, ISLAMABAD Page 8 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

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:


__________________________________________________________________________
__________________________________________________________________________
__________________________________________________________________________
Spring 2017:
NUCES, ISLAMABAD Page 9 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06

__________________________________________________________________________
___________________________
Lab Engineers signature and Date

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 labsession: LabNumber: _____, 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: _____________________
Labwrite-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


Instructio How much did you understand about the practical? -2 -1 0 +1 +2
n 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
Equipmen
t
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 10 of 10
Programming Fundamentals Lab

You might also like