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

8-Function-Recursion

The document explains the concept of recursion in programming, emphasizing its utility in breaking down complex problems into smaller, manageable instances. It outlines the components of a recursive function, including the base case and recursive case, and provides examples such as calculating factorials and checking for palindromes. Recursion is highlighted as a powerful tool for algorithms that utilize divide and conquer techniques and for traversing tree and graph structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

8-Function-Recursion

The document explains the concept of recursion in programming, emphasizing its utility in breaking down complex problems into smaller, manageable instances. It outlines the components of a recursive function, including the base case and recursive case, and provides examples such as calculating factorials and checking for palindromes. Recursion is highlighted as a powerful tool for algorithms that utilize divide and conquer techniques and for traversing tree and graph structures.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Recursive Functions

function inside function

Engr. Hiroyoshi DG. Arai


Recursion
• By definition from its root word recur;
To repeat in a periodical manner.
• In Programming, this concept is
looping.
• In OOP, Looping through functions by
calling itself to repeat is called
recursive function.
Why use recursion instead?
• Solving complex tasks will be made easier by
breaking down complex problems into
smaller instances of the same problem,
resulting in compact and readable code.
• For algorithms that require “Divide and
conquer” technique.
• For backtracking programming problem such
as sudoku solutions.
Why use recursion instead?
• Dynamic programming by combining
multiple functions that solves smaller
problems and combining them into a
complete solution.
• For tree and graph structures,
simplifying traversal and pattern
recognition.
Components of a recursive function
• Base case
– Every recursive function must have a base
case. The base case is the simplest scenario
that does not require further recursion.
– This is a termination condition that prevents
the function from calling itself indefinitely.
– Without this, recursive function can lead to
infinite recursion.
Components of a recursive function
• Recursive case:
– Here, the function calls itself with the
modified arguments. This is the essence of
recursion, solving a larger problem by
breaking it down into smaller instances of
same problem.
– The recursive case should move closer to
the base case with each iteration.
Example Problem
• In a factorial of number problem where
x! = x * x-1 * x-2 * x-3 * … * 1
• Base case should be x = 0, where the function
should return 1.
• Recursive case multiplies x with the result of
the same function with parameter x -1. This
process should continue until the base case is
reached.
Example Problem
• In a factorial of number
problem where
x! = x * x-1 * x-2 * x-3 * … * 1
• Base case should be x = 0,
where the function should
return 1 because 0! = 1.
• Recursive case multiplies x
with the result of the same
function with parameter x -1.
This process should continue
until the base case is
reached.
Example Problem
• In the case of x = 5,
since x is not equal to
zero, we will go to the
else clause of the code.
• The program wants to
return 5 * factorial(4),
but since factorial(4) is
a function, it should
evaluate it first.
Example Problem
• This process will
then repeat with
factorial(4) that
want to return 4
* factorial(3)
Example Problem
• This process will
then repeat with
factorial(3) than
want to return 3
* factorial(2)
Example Problem
• This process will
then repeat with
factorial(2) than
want to return 2
* factorial(1)
Example Problem
• This process will
then repeat with
factorial(1) than
want to return 1
* factorial(0)
Example Problem
• Here, at
factorial(0), we will
reach the base
case and will return
1.
• We will now start
backtracking the
evaluation.
Example Problem
• factorial(1) will
return 1*1 which
is equal to 1, so
we will
backtrack again.
Example Problem
• factorial(2) will
return 2*1 which
is equal to 2, so
we will
backtrack again.
Example Problem
• factorial(3) will
return 3*2 which
is equal to 6, so
we will
backtrack again.
Example Problem
• factorial(4) will
return 4*6 which
is equal to 24, so
we will
backtrack again.
Example Problem
• factorial(5) will
return 5*24 which
is equal to 120.
• Here, we reached
our recursive case
and will return the
answer of 120.
Example Problem
• Detect if the string is a palindrome or
not.
• Return True if the string is
palindrome.
• Otherise, False.
Example Problem
• Using recursion, calculate the value
of ‘a’ to the power of ‘b’ using
recursion. 𝑏
𝑎
Example Problem
• What is the nth number of Fibonacci
sequence where the first two number
of the sequence is 0 and 1
Example Problem
• Display a triangular array of binomial
coefficients of a pascal triangle.

You might also like