0% found this document useful (0 votes)
223 views

Recursion in C

Uploaded by

Samyak Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

Recursion in C

Uploaded by

Samyak Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Recursion

Recursion / Slide 2

Recursion
Recursion is the process of repeating items in a
self-similar way.
In programming languages, if a program allows
you to call a function inside the same function,
then it is called a recursive call of the function.
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
Recursion / Slide 3

Recursion
 In some problems, it may be natural to define
the problem in terms of the problem itself.
 Recursion is useful for problems that can be
represented by a simpler version of the same
problem.
 Example: the factorial function
6! = 6 * 5 * 4 * 3 * 2 * 1
We could write:
6! = 6 * 5!
Recursion / Slide 4

Example 2: factorial function


In general, we can express the factorial
function as follows:
n! = n * (n-1)!
Is this correct? Well… almost.
The factorial function is only defined for
positive integers. So we should be a bit more
precise:
n! = 1 (if n is equal to 1)
n! = n * (n-1)! (if n is larger than 1)
Recursion / Slide 5

factorial function
The C equivalent of this definition:
int fac(int numb){
if(numb==1)
return 1;
else
return numb * fac(numb-1);
}
recursion means that a function calls itself
Recursion / Slide 6

factorial function
 Assume the number typed is 3, that is, numb=3.
fac(3) :
3 == 1 ? No.
fac(3) = 3 * fac(2)
fac(2) :
2 == 1 ?
No.
fac(2) = 2 * fac(1)
fac(1) :
1 == 1 ? Yes.
return 1 int fac(int numb){
fac(2) = 2 * 1 = 2 if(numb==1)
return fac(2) return 1;
fac(3) = 3 * 2 = 6 else
return fac(3) return numb * fac(numb-1);
}
fac(3) has the value 6
Recursion / Slide 7

Copyright © 2000 by Brooks/Cole Publishing Company


A division of International Thomson Publishing Inc.
Recursion / Slide 8

factorial function
For certain problems (such as the factorial function), a
recursive solution often leads to short and elegant code.
Compare the recursive solution with the iterative solution:

Recursive solution Iterative solution

int fac(int numb){ int fac(int numb){


if(numb<=1) int product=1;
return 1; while(numb>1){
else product *= numb;
return numb*fac(numb-1); numb--;
}
}
return product;
}
Recursion / Slide 9

Recursion

We have to pay a price for recursion:


 calling a function consumes more time and memory than
adjusting a loop counter.
 high performance applications (graphic action games,
simulations of nuclear explosions) hardly ever use recursion.

In less demanding applications recursion is an


attractive alternative for iteration (for the right
problems!)
Recursion / Slide 10

Recursion
If we use iteration, we must be careful not to
create an infinite loop by accident:

for(int incr=1; incr!=10;incr+=2)


...

Oops!
int result = 1;
while(result >0){
...
result++;
}
Oops!
Recursion / Slide 11

Recursion
Similarly, if we use recursion we must be
careful not to create an infinite chain of
function calls:
int fac(int numb){ Oops!
return numb * fac(numb-1); No termination
}
condition
Or:
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
}
Oops!
Recursion / Slide 12

Recursion

We must always make sure that the recursion


bottoms out:

 A recursive function must contain at least one


non-recursive branch.
 The recursive calls must eventually lead to a
non-recursive branch.
Recursion / Slide 13

Recursion

 Recursion is one way to decompose a task into


smaller subtasks. At least one of the subtasks is
a smaller example of the same task.
 The smallest example of the same task has a
non-recursive solution.

Example: The factorial function


n! = n * (n-1)! and 1! = 1
Recursion / Slide 14

Direct Computation Method


 Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
where each number is the sum of the preceding
two.

 Recursive definition:
 F(0) = 0;
 F(1) = 1;
 F(number) = F(number-1)+ F(number-2);
Recursion / Slide 15

Example 2: Fibonacci numbers


//Calculate Fibonacci numbers using recursive function.
//A very inefficient way, but illustrates recursion well
int fib(int number)
{
if (number == 0) return 0;
if (number == 1) return 1;
return (fib(number-1) + fib(number-2));
}
int main(){ // driver function
int inp_number;
printf("Please enter an integer: “);
scanf(“%d”, &inp_number;
printf("The Fibonacci number for %d is “,inp_number, fib(inp_number));
return 0;
}
Recursion / Slide 16

Copyright © 2000 by Brooks/Cole Publishing Company


A division of International Thomson Publishing Inc.
Example 3: Fibonacci number w/o
Recursion / Slide 17

recursion

//Calculate Fibonacci numbers iteratively


//much more efficient than recursive solution

int fib(int n)
{
int f[100];
f[0] = 0; f[1] = 1;
for (int i=2; i<= n; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
Recursion / Slide 18

Recursion General Form


 How to write recursively?

int recur_fn(parameters){
if(stopping condition)
return stopping value;
// other stopping conditions if needed
return function of recur_fn(revised parameters)

}
Recursion / Slide 19

Copyright © 2000 by Brooks/Cole Publishing Company


A division of International Thomson Publishing Inc.
Recursion / Slide 20

Example 5: exponential func


 How to write exp(int numb, int power)
recursively?

int exp(int numb, int power){


if(power ==0)
return 1;
return numb * exp(numb, power -1);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy