Recursion. A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for...

9
Recursion

Transcript of Recursion. A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for...

Page 1: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Recursion

Page 2: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Recursion A recursive function contains a call to itself

Example: the factorialn!=n*(n-1)! for n>1n!=1 for n=1

int factorial (int n) {

if (n == 0) { return 1; } else { return (n * factorial (n-1)); }}

Page 3: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Stacks and The Recursive Call Computers use a structure called a stack

to keep track of recursion A stack is a memory structure analogous to a

stack of paper: Last In First Out Whenever a function is called, the computer

uses a "clean sheet of paper“ called active frame The function definition is copied to the paper The arguments are plugged in for the parameters The computer starts to execute the function body

Page 4: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Recursion Key elements of a recursive function

The function calls itself There must be some terminal condition that

ends the recursion Otherwise you will call yourself infinitely and the

program will blow up The recursion should be getting simpler on each

call

Page 5: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Examples power function:

int power(int base, int exponent); power (2,4)=2*2*2*2

power(2,4)=2*power(2,3)power(x,n)=x*power(x,n-1) (for n>0)

=1 (for n=0)int power (int base, int exponent) {

fill in the space

};

Page 6: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

power function int power(int base, int exponent) {

If (exponet==0)return 1;else If (exponet>0)

return (base*power(base, exponent-1));else {cout<<“we only take care of positive

exponent”<<endl;exit(1) }}

Page 7: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Fibonacci sequence

Fibonacci sequence

12

1

0

.

.

,1

,1

nnn aaa

a

a

Page 8: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Fibonacci sequence functionint fibonacci(int n) {

if (n == 1) { return 1; } else if (n == 2) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); }

}

Page 9: Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)

Recursion and iteration Any task that can be accomplished using

recursion can also be done without recursion A nonrecursive version of a function typically

contains a loop or loops A non-recursive version of a function is usually

called an iterative-version A recursive version of a function

Usually runs slower Uses more storage May use code that is easier to write and understand