Download presentation
Presentation is loading. Please wait.
Published byDortha Hancock Modified over 9 years ago
1
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental
2
Lecture outline Recursion Function overloading
3
Recursion
4
A recursive function is a function that calls itself. [ [ Recursive problem-solving approaches have a number of elements in common. Recursive Function has two parts (i)Base Case(s). The function actually knows how to solve only the simplest case(s), If the function is called with a base case, the function simply returns a result. (ii) Complex Case(s) If the function is called with a more complex problem, It typically divides the problem into two conceptual pieces. a)A piece that the function knows how to do and b)A piece that it does not know how to do
5
Complex Case(s) To make recursion feasible, the latter piece must resemble the original problem But be a slightly simpler or slightly smaller version. Function launches (calls) a fresh copy of itself to work on the smaller problem this is referred to as a recursive call and is also called the recursion step. The recursion step often includes the keyword return,
6
Recursion In order for the recursion to eventually terminate, each time the function calls itself with a slightly simpler version of the original problem, this sequence of smaller and smaller problems must eventually converge on the Base Case. At that point, the function recognizes the base case and returns a result to the previous copy of the function, and a sequence of returns ensues all the way up the line until the original function call eventually returns the final result to main.
7
Special function which can call itself x 10 = x * x 9 x 9 = x * x 8 x 8 = x * x 7 … x n = x * x n-1 Recursive Functions
8
n! = n * (n-1) * (n-2) …….. 3 * 2 * 1 5! = 5 * 4 * 3 * 2 * 1 4! = 4 * 3 * 2 * 1 5! = 5 * 4! 0! = 1 Recursive Functions: Factorial
9
long factorial ( long n ) { if (n == 1 ) return ( n ) ; else return ( n * factorial (n-1) ) ; } Recursive Functions: Factorial
11
Try to write program for Fibonacci series Exercise
12
Set of recursive calls to function fibonacci f( 3 ) f( 1 ) f( 2 ) f( 1 )f( 0 )return 1 return 0 return + + Example The Fibonacci Series
13
long fab (long); main() { for(int i=0; i<=10; i++) { cout<<"fabonaci"<<i<<"="<<fab(i)<<endl; } getch(); } long fab(long x) { if (x==1 || x==0) return x; else return (fab(x-1)+fab(x-2)); }
14
Overloading
15
Function Overloading Use Functions more than one with same Name is called Function Overloading. Function overloading is commonly used to create several functions of the same name that perform similar tasks, but on different data types. Overloaded functions are distinguished by their signatures.
16
Function Overloading A signature is a combination of a function's name and its parameter types (in order). When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call.
17
Function Overloading int saquare(int); double saquare(float); Main() { int x, Float y; Cout<<“ enter number to calculate saqure”; Cin>> x; Cout<< enter number to claculate saqure”; Cin>> y; Cout<< “area=“<<square(x); Cout<< “area=“<<square(y); } int square(int i); { Return(i*i); } double square(flaot i); { Return(i*i); }
18
Function Overloading #include int area( int, int); double area(double, double); main() { cout<<area(3.1,4.2); getch(); } int area(int x, int y) { return(x+y); } double area(double x, double y) { return(x+y); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.