Download presentation
Presentation is loading. Please wait.
Published byCollin McGee Modified over 9 years ago
1
Object Oriented Programming Spring - 2012 COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah kaleemullah@ciitvehari.edu.pk
2
Overloaded functions int main() { repchar(); repchar(‘=’); repchar(‘+’, 30); return 0; } 2
3
Overloaded functions 3
4
Overloaded functions: Different kinds of arguments struct Distance { int feet; float inches; }; Void disp( Distance ); //declarations Void disp( float ); 4
5
Overloaded functions: Different kinds of arguments struct Distance { int feet; float inches; }; Void disp( Distance ); //declarations Void disp( float ); 5
6
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
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
8 Power function Lets figure out a recursive function for calculating the Powers of a given number 2 nd power function Square of x = x*x 3 rd power function Cube of x = x*x*x 4th power function Fourth Power of x = x*x*x*x
9
9 Power function x 4 = x*x*x*x = x*( x*x*x ) = x*x 3 x 5 = x*x*x*x*x = x*(x*x*x*x ) = x*x 4 x 6 = x*x*x*x*x*x = x*(x*x*x*x*x ) = x*x 5 In general x n = x*x n-1 Int power (int x, int n) { return x * power (x, n-1) }
10
10 Power Function When does it stop ? Int power (int x, int n) { return x * power (x, n-1) } Step no. Calc 2 3 : 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 2 0 =1 Base case: if n==0 return 1 Trace table
11
11 Revised Power Function Int 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 2 3 : 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
12 Factorial function Factorial0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24
13
13 Factorial function 0! = 1 1! = 1 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 4*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
14 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) 1 1 2 6 =24
15
15 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
16
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
17 Function for fibonacci sequence Int fib (int n) { If (n==0 or n==1) return 1; else return fib(n-1) +fib (n-2); }
18
18 Trace of Fibonacci(5) fib 5 fib 4 + fib 3 fib 3 + fib 2 fib 2 + fib 1 fib 1 + fib 0 = 8 If (n==0 or n==1) return 1; else return fib(n-1) +fib (n-2); 32 53 2 2 1 1 1 1 1 1 1 1
19
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 20
21
In-Line functions 21
22
In-Line functions: Example #include 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; } 22
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. 23
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 using namespace std; void repchar(char=’*’, int=45); //declaration with default arguments 24
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; } 25
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; } 26
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 27
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 28
29
Static Variable: Example void teststatic() { static int count; //automatically initialized to 0 count++; cout<<"I am called "<<count<<" times"<<endl; } void main() { teststatic(); } 29
30
Static Variable: Example 30
31
Non-Static Variable: Example void teststatic() { int count=0; // initialized to 0, not a static variable count++; cout<<"I am called "<<count<<" times"<<endl; } int main() { teststatic(); } 31
32
Non-Static Variable: Example 32
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); } 33
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.