lecture-13 instructor name: muhammad safyan programming fundamental

18
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Upload: dortha-hancock

Post on 13-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Lecture-13

Instructor Name: Muhammad Safyan

Programming Fundamental

Page 2: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Lecture outline

Recursion

Function overloading

Page 3: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Recursion

Page 4: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Recursion

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

Page 5: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

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,

Page 6: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

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.

Page 7: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

• Special function which can call itself

x10 = x * x9

x9 = x * x8

x8 = x * x7

… …

xn = x * xn-1

Recursive Functions

Page 8: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

n! = n * (n-1) * (n-2) …….. 3 * 2 * 15! = 5 * 4 * 3 * 2 * 14! = 4 * 3 * 2 * 1

5! = 5 * 4!

0! = 1

Recursive Functions: Factorial

Page 9: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

long factorial ( long n ){

if (n == 1 )return ( n ) ;

else return ( n * factorial (n-1) ) ;

}

Recursive Functions: Factorial

Page 10: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental
Page 11: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Try to write program for

• Fibonacci series

Exercise

Page 12: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

• Set of recursive calls to function fibonacci

f( 3 )

f( 1 )f( 2 )

f( 1 ) f( 0 ) return 1

return 1 return 0

return +

+return

Example The Fibonacci Series

Page 13: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Example The Fibonacci Series

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)); }

Page 14: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Overloading

Page 15: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

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.

Page 16: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

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.

Page 17: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

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);

}

Page 18: Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental

Function Overloading

#include<iostream.h> #include<conio.h> 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);}