290 likes | 395 Views
COMSATS Institute of Information Technology. Object Oriented Programming Spring - 2012. Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah kaleemullah@ciitvehari.edu.pk. Overloaded functions. Same function name Perform different operations for different types of data
E N D
COMSATS Institute of Information Technology Object Oriented ProgrammingSpring - 2012 Functions OOP in C++ by Robert Lafore - Chapter#5 KaleemUllah kaleemullah@ciitvehari.edu.pk
Overloaded functions • Same function name • Perform different operations for different types of data • different data passed • Example: same name, different number of arguments • void repchar(); //declarations • void repchar(char); • void repchar(char, int);
Overloaded functions int main() { repchar(); repchar(‘=’); repchar(‘+’, 30); return 0; }
Overloaded functions // repchar() // displays 45 asterisks void repchar() { for(int j=0; j<45; j++) // always loops 45 times cout << ‘*’; // always prints asterisk cout << endl; }
Overloaded functions • // displays 45 copies of specified character • void repchar(char ch) • { • for(int j=0; j<45; j++) // always loops 45 times • cout << ch; // prints specified character • cout << endl; • }
Overloaded functions • // displays specified number of copies of specified character • void repchar(char ch, int n) • { • for(int j=0; j<n; j++) // loops n times • cout << ch; // prints specified character • cout << endl; • }
Overloaded functions: Different kinds of arguments • struct Distance • { • int feet; • float inches; • }; Void disp( Distance ); //declarations Void disp( float );
Overloaded functions: Different kinds of arguments • struct Distance • { • int feet; • float inches; • }; Void disp( Distance ); //declarations Void disp( float );
Recursion What is recursion? Recursion See “Recursion”. Recursion If you still don't get it, see "Recursion"
Recursion Void fun() { cout<<“this is fun”<<endl; fun(); }
Recursion Void fun() { cout<<“this is fun”<<endl; fun(); } Example of program to print numbers in descending order? When will this end?
Recursion Example of program to print numbers in descending order? int print (int n) { cout<< n<<endl; if (n>1) print(--n); else return 1; } void main () { print (10); // start recursion by calling recursive function }
Properties of recursive functions • Base Case(s): condition for terminating the recursive process • Recursive Case(s): set of rules to break the problem into smaller sub-problems to reach base case • Divide the problem into smaller sub-problems • Solve the sub-problems • Combine results to get answer Sub-problems solved as a recursive call to the same function
Need of Base Case and Recursive Case int loop(int x) { return (1 + loop(x)) } • Recursive function with • no base case • not a valid recursive case • Trace Table with x=5 Problem not being divided into smaller problems – no termination loop 5 1 + loop 5 1 + loop 5 infinite loop – no termination …
Power function • Lets figure out a recursive function for calculating the Powers of a given number • 2nd power function • Square of x = x*x • 3rd power function • Cube of x = x*x*x • 4th power function • Fourth Power of x = x*x*x*x
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) }
Power Function Int power (int x, int n) { return x * power (x, n-1) } Trace table x * power (x, n-1) We know 20=1 Base case: if n==0 return 1 We need to stop here When does it stop ?
Revised Power Function Int power (int x, int n) { If (n==0) return 1; else return x * power (x, n-1) } Base case 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) =8 4 2 1 Recursive case Result: 8 sub-problems must be “smaller” than the original problem otherwise the recursion never terminates.
Factorial function Factorial 0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24
Factorial function 0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 1*0! 2*1! • 3*2! • 4*3! • …… • 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
Factorial function • Int 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) =24 6 2 1 1
Factorial function Version Action Argument or Return Value 1 Call 5 2 Call 4 3 Call 3 4 Call 2 5 Call 1 5 Return 1 4 Return 2 3 Return 6 2 Return 24 1 Return 120
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)
Function for fibonacci sequence • Int fib (int n) • { • If (n==0 or n==1) • return 1; • else • return fib(n-1) +fib (n-2); • }
Trace of Fibonacci(5) • If (n==0 or n==1) • return 1; • else • return fib(n-1) +fib (n-2); = 8 fib 5 5 3 fib 4 + fib 3 3 2 1 fib 3 + fib 2 fib 2 + fib 1 2 fib 1 + fib 0 fib 1 + fib 0 fib 2 + fib 1 2 1 1 1 1 1 1 fib 1 + fib 0 1
Why recursion? • Recursion makes the program faster? • Recursion uses less memory? • Recursion makes the code much simpler and Easy to read