Professional Documents
Culture Documents
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
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.
int main(void){
int A = 1;
double B = 1.1;
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
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;
}
}
Spring 2017:
NUCES, ISLAMABAD Page 3 of 10
Programming Fundamentals Lab
Function Overloading and Recursive Functions LAB 06
times.
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:
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
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>
{
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;
smallest(x, y);
smallest(t, h);
Spring 2017:
NUCES, ISLAMABAD Page 8 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
Spring 2017:
NUCES, ISLAMABAD Page 10 of 10
Programming Fundamentals Lab