0% found this document useful (0 votes)
38 views15 pages

Recursion

Recursion is a repetitive process where a function calls itself. It requires a base case and each recursive call must make progress towards the base case. Recursive functions are designed with an base case, induction step, and algorithm combining the two. Examples given are calculating factorials and Fibonacci numbers recursively.

Uploaded by

leena sakri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views15 pages

Recursion

Recursion is a repetitive process where a function calls itself. It requires a base case and each recursive call must make progress towards the base case. Recursive functions are designed with an base case, induction step, and algorithm combining the two. Examples given are calculating factorials and Fibonacci numbers recursively.

Uploaded by

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

Recursion

• Recursion is a repetitive process in which an


algorithm calls itself. by and large recursion is
organized in such a way that a function or
subroutine calls itself.
Content of a Recursive Method
• Base case(s):Values of the input variables for
which we perform no recursive calls are called
base cases (there should be at least one base
case).
• Every possible chain of recursive calls must
eventually reach a base case.
• Recursive calls:
• This is calls to the current method.
• Each recursive call should be defined so that it
makes progress towards a base case.
• The recursive solution for the given problem
involves in a two-way journey,
– firstly we decompose the problem from the top to
the bottom and
– then we solve the problem from the bottom to the
top.
How Recursive Works ?
• In a recursive function execution process, each call sets up
a new instance of all the parameters and the local
variables.
• As always, when the method completes, control returns to
the method that invoked it (which might be another
invocation of the same method)
• Example :
• pow(4, 3) = 4 * pow(4, 2)
• = 4 * 4 * pow(4, 1)
• = 4 * 4 * 4 * pow(4, 0)
• =4*4*4*1
• = 64
• Recursive functions are designed in 3 steps
• Step 1. Identify a base case an instance of problem whose
solution is trivial
• Ex: The factorial function has two base cases:
– if n = 0 : n! = 1
– if n = 1 : n! = 1
• Step 2. Identify an induction step: a means of solving non trivial
instance of problem using one or more smaller instances of
problem
• Ex: In the factorial problem, we solve the big problem using a smaller
version of the problem, n! = (n-1)! n
• Step 3: Form an algorithm from the base and induction step
• Algorithm to compute factorial
• Factorial (N)
1. Receive N
2. if N > 1 return
Factorial(N-1) * N
else
return 1
Example 1: Fibonacci numbers series.
• The computation of fibonacci series, each next
number is equal to the
• sum of the previous two numbers. A classical
Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, …

You might also like