Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | collin-mcgee |
View: | 246 times |
Download: | 4 times |
Object Oriented ProgrammingSpring - 2012
COMSATS Institute of Information Technology
FunctionsOOP in C++ by Robert Lafore - Chapter#5
Kaleem [email protected]
2
Overloaded functions
int main()
{
repchar();
repchar(‘=’);
repchar(‘+’, 30);
return 0;
}
3
Overloaded functions
4
Overloaded functions: Different kinds of arguments
• struct Distance • {• int feet;• float inches;• };
Void disp( Distance ); //declarations
Void disp( float );
5
Overloaded functions: Different kinds of arguments
• struct Distance • {• int feet;• float inches;• };
Void disp( Distance ); //declarations
Void disp( float );
6
Properties of recursive functions
1. Base Case(s): condition for terminating the recursive process
2. Recursive Case(s): set of rules to break the problem into smaller sub-problems to reach base case
a) Divide the problem into smaller sub-problems
b) Solve the sub-problems
c) Combine results to get answer
Sub-problems solved as a recursive call to the same function
7
int loop(int x) {
return (1 + loop(x))}
infinite loop – no termination
• Trace Table with x=5
1 + loop 5
Problem not being divided into smaller problems – no termination
1 + loop 5
…
loop 5
Need of Base Case and Recursive Case
Recursive function with
– no base case
– not a valid recursive case
8
Power function• Lets figure out a recursive function for
calculating the Powers of a given number
2nd power functionSquare of x = x*x
3rd power functionCube of x = x*x*x
4th power functionFourth Power of x = x*x*x*x
9
Power function
x4 = x*x*x*x = x*( x*x*x ) = x*x3
x5 = x*x*x*x*x = x*(x*x*x*x ) = x*x4
x6 = x*x*x*x*x*x = x*(x*x*x*x*x ) = x*x5
In general
xn = x*xn-1
Int power (int x, int n)
{
return x * power (x, n-1)
}
10
Power Function
When does it stop ?
Int power (int x, int n)
{
return x * power (x, n-1)
}
Step no.Calc 23: Power (2,3)x=2, n=3
1 2* power(2,2)
2 2* 2* power(2,1)
3 2*2* 2*power(2,0)
4 2*2*2* 2*power(2,-1)
x * power (x, n-1)
We need to stop here
We know 20=1
Base case: if n==0 return 1
Trace table
11
Revised Power FunctionInt power (int x, int n)
{
If (n==0)
return 1;
else
return x * power (x, n-1)
}
Result: 8
Base case
Recursive case
sub-problems must be “smaller” than the original problem otherwise the recursion never terminates.
Trace table: Calc 23: x=2, n=3
Power (2,3)
2* power(2,2)
2* 2* power(2,1)
2*2* 2*power(2,0)
1
2
4
=8
12
Factorial function
Factorial 0! = 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
13
Factorial function
0! = 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 244*3!
3*2!
2*1!
1*0!
……
In general: n!=n*(n-1)!
Recursive case: Factorial(n)=n*factorial(n-1)
Base case: 0!=1 i.e; if (n==0) return 1
14
Factorial functionInt factorial (int n){If (n==0)
return 1;else
return n * factorial (n-1)}
Trace table: Calc 4! here n=4
factorial (4)
4* factorial (3)
4* 3* factorial (2)
4*3* 2* factorial (1)
4*3*2* 1* factorial (0)11
26
=24
15
Factorial function
Version Action Argument or Return Value1 Call 52 Call 43 Call 34 Call 25 Call 15 Return 14 Return 23 Return 62 Return 241 Return 120
16
Fibonacci sequence
The first ten terms in the sequence are:
1,1,2,3,5,8,13,21,34,55
Each value, except for first two, is sum of last two values
Simply saying:Fib(n)= fib(n-1)+fib(n-2) except for when n=0 and n=1
Base case: if (n==0 or n==1)Return 1
Recursive case: Fib(n)= fib(n-1)+fib(n-2)
17
Function for fibonacci sequenceInt fib (int n){If (n==0 or n==1)
return 1;else
return fib(n-1) +fib (n-2);}
18
Trace of Fibonacci(5)
fib 5
fib 4 + fib 3
fib 3 + fib 2 fib 2 + fib 1
fib 2 + fib 1 fib 1 + fib 0 fib 1 + fib 0
fib 1 + fib 0
= 8
If (n==0 or n==1)return 1;elsereturn fib(n-1) +fib (n-2);
3 2
5 3
2
2 1
11
1
1
111
19
Why recursion?• Recursion makes the
program faster?• Recursion uses less
memory?
• Recursion makes the code much simpler and Easy to read
20
In-Line functions• Functions save memory space because
call to function cause the same code to be executed
• When a function is called, jump to function is made. After the call, jump back to instruction following the call
• takes some extra time for jump to function Slows down the program
• To save execution time for short functions having one or two statements) Make them inline
21
In-Line functions
22
In-Line functions: Example#include <iostream>
using namespace std;
inline float lbstokg(float pounds) // converts pounds to kilograms
{
return 0.453592 * pounds;
}
int main()
{
float lbs;
cout << “\nEnter your weight in pounds: “;
cin >> lbs;
cout << “Your weight in kilograms is “ << lbstokg(lbs)
<< endl;
return 0;
}
23
In-Line functions: Example• inline float lbstokg(float pounds) // inline function
• Sometimes the compiler will ignore the request and compile the function as a normal function.
• It might decide the function is too long to be inline, for instance.
24
Default arguments• In OVERLOAD we used three different functions
with the same name to handle different numbers of arguments.
• Achieve the same effect in a different way!!
// demonstrates missing and default arguments
#include <iostream>
using namespace std;
void repchar(char=’*’, int=45); //declaration with default arguments
25
Default arguments: Example• Achieve the same effect in a different way!!
int main()
{
repchar(); //prints 45 asterisks
repchar(‘=’); //prints 45 equal signs
repchar(‘+’, 30); //prints 30 plus signs
return 0;
}
26
Default arguments: Example
void repchar(char ch, int n)
{
for(int j=0; j<n; j++) //loops n times
cout << ch; //prints ch
cout << endl;
}
27
Default arguments: Example
void repchar(char=’*’, int=45);
//declaration with default arguments
• The default argument follows an equal sign• Working in case of missing arguments?
One missing? Two missing? Remember that missing arguments must be those at the end of the
argument list!! Can’t miss first argument and provide second
28
Static Variable• A static variable in kind of local variable (inside a function)
lifetime is the same as that of a global variable
• Does not exist until the first call to the function containing it is made
• remains in existence for the life of the program
29
Static Variable: Examplevoid teststatic()
{
static int count; //automatically initialized to 0
count++;
cout<<"I am called "<<count<<" times"<<endl;
}
void main()
{
teststatic();
teststatic();
teststatic();
}
30
Static Variable: Example
31
Non-Static Variable: Examplevoid teststatic()
{
int count=0; // initialized to 0, not a static variable
count++;
cout<<"I am called "<<count<<" times"<<endl;
}
int main()
{
teststatic();
teststatic();
teststatic();
}
32
Non-Static Variable: Example
33
Const function arguments• To assure that the function cannot change the value
passed
void testconst(int a, const int b )
{
a=10; //OK
b=10; //error
}
int main()
{
int x,y;
testconst(x,y);
}