This document discusses recursion in programming. It defines recursion as a technique for solving problems by repeatedly applying the same procedure to reduce the problem into smaller sub-problems. The key aspects of recursion covered include recursive functions, how they work by having a base case and recursively calling itself, examples of recursive functions in Python like calculating factorials and binary search, and the differences between recursion and iteration approaches.
Introduction to recursion, learning objectives including recursive functions, their workings, Python implementation, examples, and recursion vs iteration.
Definitions of functions as reusable code blocks, types of functions in Python including built-in and user-defined functions.
Recursion as a problem-solving method, types of recursion (direct & indirect), and concerns like memory usage.
How recursive functions operate, importance of base cases, and examples like calculating factorial.
Guide on writing recursive functions in Python including base cases, recursive cases, and Fibonacci sequence example.
Explanation of binary search as a recursive algorithm and its operations on sorted arrays.
Differences between recursion and iteration based on definitions, applications, terminations, and time complexities.
Recap of the topics learned including functions, recursion in Python, and comparison between recursion and iteration.
2
Learning objectives
⢠Introduction
â˘Recursive Functions
⢠How Recursive Works
⢠Recursive in Python
⢠Recursive functions Examples
⢠Recursive Vs Iteration
3.
3
What Are Functions?
â˘A function is a block of code which only runs when it is
called.
⢠Functions are sub-programs which perform tasks which may
need to be repeated.
⢠Some functions are âbundledâ in standard libraries which are
part of any languageâs core package. Weâve already used
many built-in functions, such as input(), eval(), etc.
⢠Functions are similar to methods, but may not be connected
with objects
⢠Programmers can write their own functions
4.
4
Types of Functions
Differenttypes of functions in Python:
Python built-in functions, Python recursion function, Python
lambda function, and Python user-defined functions with their
syntax and examples.
5.
5
Recursion:
⢠A techniquefor solving a large computational problem
by repeatedly applying the same procedure to reduce it
to successively smaller problems.
⢠Recursion refers to a programming technique in which
a function calls itself either directly or indirectly
⢠Recursion is a common mathematical and programming
concept.
⢠A recursive procedure has two parts:
ď§ One or more base cases
ď§ A recursive steps.
Recursion
6.
6
⢠This hasthe benefit of meaning that you can loop through
data to reach a result.
⢠It means that a function calls itself.
⢠Recursion can be two types:
ď§ Direct Recursion
ď§ Indirect Recursion
⢠The developer should be very careful with recursion as it can
be quite easy to slip into writing a function which never
terminates, or one that uses excess amounts of memory or
processor power.
Recursion
7.
7
⢠Direct Recursion:if function calls itself directly from its
function body.
Example:
def recur():
recur() # function recur() calling itself
⢠Indirect Recursion: if a function calls another function,
which calls its caller function
Example:
def recur-A():
recur-B() # function recur-A() calling recur-B(),
which calls recur-A()
def recur-B():
recur-A()
Recursion
8.
8
Overview of howrecursive function works:
⢠Recursive function is called by some external code.
⢠If the base condition is met then the program do something meaningful
and exits.
⢠Otherwise, function does some required processing and then call itself to
continue recursion. Here is an example of recursive function used to
calculate factorial.
⢠Example:
⢠Factorial is denoted by number followed by (!) sign i.e 4!
⢠Steps:
ď§ 4! = 4 * 3 * 2 * 1
ď§ 2! = 2 * 1
ď§ 0! = 1
How Recursive Works
9.
9
⢠However, whenwritten correctly recursion can be a very efficient and
mathematically-elegant approach to programming.
⢠Sensible Recursive code is the one that fulfills following requirements :
ď§ It must have a case, whose result is known or computed without any recursive
calling -The BASE CASE.
ď§ The BASE CASE must be reachable for some argument/parameter.
ď§ it also have Recursive Case, where by function calls itself.
⢠Example:
def factorial_recursive(n):
# Base case: 1! = 1
if n == 1:
return 1
# Recursive case: n! = n * (n-1)!
else:
return n * factorial_recursive(n-1)
print("nn Recursion Example Results")
factorial_recursive(6)
How Recursive Works
10.
10
Recursive in Python
Writinga Recursive Function.
⢠Before you start working recursive functions, you must know that every
recursive function must have at least two cases :
ď§ The Recursive Case (or the inductive case)
ď§ The Base Case (or the stopping case)always required
⢠The Base Case in a recursive program must be reachable that causes
the recursion to end.
⢠The Recursive Case is the more general case of the problem we're trying
to solve using recursive call to same function.
⢠Example: function xn, the recursive case would be :
Power (x, n) = x * Power (x, n - 1)
The base cases would be:
Power(x, n)=x when n=1
Power(x, n)=1 when n=0
Other cases(when n<0) ignoring simplicity sake
11.
11
Recursive in Python
Writinga Recursive Function.
⢠The Fibonacci numbers are easy to write as a Python function.
⢠It's more or less a one to one mapping from the mathematical
definition:
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
The order in which the functions are called.
fib() is substituted by fib().
12.
12
Binary Search
Binary SearchTechniques.
⢠Popular algorithm that used recursion successfully is binary search
algorithm.
⢠Binary search works for only sorted array whereas linear search work for
both sorted as well as unsorted array.
⢠The process of binary search is illustrated in the figure:
13.
13
Binary Search
Binary SearchAlgorithm.
⢠Popular algorithm that used recursion successfully is binary search
algorithm.
14.
14
Binary Search
Binary SearchAlgorithm.
⢠Popular algorithm that used recursion successfully is binary search
algorithm.
15.
15
Recursion vs. Iteration
Differencebetween Recursion and Iteration
⢠A program is called recursive when an entity calls itself.
⢠A program is call iterative when there is a loop (or repetition).
PROPERTY RECURSION ITERATION
Definition Function calls itself. A set of instructions repeatedly
executed.
Application For functions. For loops.
Termination Through base case, where there
will be no function call.
When the termination condition for
the iterator ceases to be satisfied.
Usage Used when code size needs to be
small, and time complexity is not
an issue.
Used when time complexity needs
to be balanced against an expanded
code size.
Code Size Smaller code size Larger Code Size.
Time
Complexity
Very high(generally exponential)
time complexity.
Relatively lower time complexity
(generally polynomial-logarithmic).
16.
16
⢠We learnedabout the Python function.
⢠Recursive Functions
⢠How Recursive Works
⢠Recursive in Python
⢠Recursive functions Examples
⢠Recursive Vs. Iteration
Thank you
Conclusion!