ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalGE-167Recursive Functions
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    GE-167

    Discrete Structures

    Progress0 / 67 done
    Topics67
    1Propositional and Predicate Logic
    2Logical Operators3Translations Between Symbolic Expressions and Formal English Expression4Logical Equivalences
    5Quantifiers6Nested Quantification7Equivalences in Predicate Logic8Translations Between Symbolic Forms and Formal English
    9Proof Methods and Strategies10Direct Proof11Proof by Contraposition12Proof by Induction13Proof by Implication14Existence Proof15Uniqueness Proofs16Trivial Proofs17Vacuous Proofs
    18Notations and Set Operations19Venn Diagrams20Countable and Uncountable Sets
    21Equivalence Relations and Partitions22Partial Orderings23Recurrence Relations51Reflexive, Symmetric, Transitive, and Antisymmetric52Equivalence Relations and Equivalence Classes53Partial Orders
    24Injective, Surjective, Bijective25Special Types of Functions26Function Composition27Inverse Functions28Recursive Functions29Compositions
    30Sequences and Series
    31Inclusion and Exclusion Principle32Pigeonhole Principle33Permutations and Combinations46Product Rule and Sum Rule47Principle of Inclusion-Exclusion48Binomial Coefficients49Pascal's Identity and Pascal’s Triangle50Binomial Theorem
    34Division Theorem35Modular Arithmetic36LCM and GCD37Euclidean and Extended Euclidean Method38Finding Solutions to Congruence
    39Fundamental Theorem of Arithmetic40Characterizations of Primes41Mersenne Primes
    42Weak Induction43Strong Induction
    44Formulation of Recurrences45Closed Formulas
    54Terminologies55Elements of Graph Theory56Planar Graphs57Graph Coloring58Euler Graph59Hamiltonian Path60Rooted Trees61Graph Traversals62Handshaking Lemma and Corollary63Special Families of Graphs64Graph Isomorphism65Planarity in Graphs66Eulerian and Hamiltonian Graphs67Trees in Graph Theory
    9 min read
    Topic 28 of 67
    Topic Study Note Illustration
    28

    Recursive Functions

    A recursive function is a function that calls itself as part of its execution. In mathematical terms and computer science, recursion is a method of solving problems...

    Recursive Functions

    A recursive function is a function that calls itself as part of its execution. In mathematical terms and computer science, recursion is a method of solving problems where the solution to a problem depends on solutions to smaller instances of the same problem.

    Recursive functions are a fundamental concept in both mathematics and computer science. They allow complex problems to be broken down into simpler, manageable sub-problems that follow the same structure.


    1. Formal Definition of Recursive Functions

    In general, a recursive function f(n)f(n)f(n) is defined in terms of itself, and consists of two key components:

    1. Base Case(s): One or more simple conditions that do not require further recursion. These provide the stopping condition for the recursion.
    2. Recursive Case: An expression where the function calls itself with modified arguments, typically smaller or simpler values.

    The function continues to call itself until it reaches the base case, at which point the recursion stops and the function begins returning results back up the call stack.

    Mathematical Example

    Consider the factorial function, n!n!n!, which is commonly defined recursively:

    n!={1,if n=0n×(n−1)!,if n>0n! = \begin{cases} 1, & \text{if } n = 0 \\ n \times (n - 1)!, & \text{if } n > 0 \end{cases}n!={1,n×(n−1)!,​if n=0if n>0​

    Here:

    • Base case: 0!=10! = 10!=1.
    • Recursive case: n!=n×(n−1)!n! = n \times (n-1)!n!=n×(n−1)! for n>0n > 0n>0.

    The factorial function repeatedly reduces the problem of calculating n!n!n! into smaller sub-problems until it reaches the base case.


    2. Recursive Functions in Computer Science

    In computer science, recursion is a technique used in algorithms to solve problems. A recursive function solves a problem by solving smaller instances of the same problem. Each recursive call adds a new layer to the call stack, which must eventually return to the base case to terminate the recursion.

    Structure of a Recursive Function

    A recursive function typically has the following structure:

    def function_name(parameters):
        if base_case_condition:       # Base case
            return base_case_value
        else:
            return recursive_case_value
    
    • Base case: The simplest instance of the problem that does not involve recursion (terminates the recursion).
    • Recursive case: The part where the function calls itself with modified parameters, usually smaller in size or simpler.

    Example: Factorial Function in Python

    Here’s an implementation of the factorial function in Python:

    def factorial(n):
        if n == 0:  # Base case
            return 1
        else:
            return n * factorial(n - 1)  # Recursive case
    

    Explanation:

    • The base case is when n=0n = 0n=0, returning 111.
    • The recursive case reduces the problem to n×(n−1)!n \times (n-1)!n×(n−1)!, calling the function with n−1n-1n−1.

    3. Examples of Recursive Functions

    Let’s explore more examples of recursive functions to better understand the concept.

    Example 1: Fibonacci Sequence

    The Fibonacci sequence is defined recursively as:

    F(0)=0,F(1)=1,F(n)=F(n−1)+F(n−2) for n≥2.F(0) = 0, \quad F(1) = 1, \quad F(n) = F(n-1) + F(n-2) \text{ for } n \geq 2.F(0)=0,F(1)=1,F(n)=F(n−1)+F(n−2) for n≥2.

    This can be implemented as a recursive function:

    def fibonacci(n):
        if n == 0:  # Base case 1
            return 0
        elif n == 1:  # Base case 2
            return 1
        else:
            return fibonacci(n - 1) + fibonacci(n - 2)  # Recursive case
    

    Explanation:

    • The base cases are F(0)=0F(0) = 0F(0)=0 and F(1)=1F(1) = 1F(1)=1.
    • The recursive case calculates the next Fibonacci number by summing the two previous numbers in the sequence.

    Example 2: Sum of Integers

    The sum of the first nnn integers can be defined recursively as:

    S(n)=0if n=0,S(n) = 0 \quad \text{if } n = 0,S(n)=0if n=0, S(n)=n+S(n−1)if n>0.S(n) = n + S(n-1) \quad \text{if } n > 0.S(n)=n+S(n−1)if n>0.

    This can be implemented as:

    def sum_integers(n):
        if n == 0:  # Base case
            return 0
        else:
            return n + sum_integers(n - 1)  # Recursive case
    

    Explanation:

    • The base case is when n=0n = 0n=0, returning 0.
    • The recursive case adds nnn to the sum of integers from 000 to n−1n-1n−1.

    4. Tail Recursion and Head Recursion

    There are two main types of recursion based on where the recursive call is placed:

    a. Tail Recursion

    A recursive function is tail recursive if the recursive call is the last operation in the function. In tail recursion, there is no need to keep the current function’s state in memory once the recursive call is made. This can be more efficient in some programming languages because it allows for tail call optimization (where the system reuses the current function’s stack frame).

    Example: Tail-recursive factorial function:

    def factorial_tail_recursive(n, accumulator=1):
        if n == 0:
            return accumulator
        else:
            return factorial_tail_recursive(n - 1, n * accumulator)
    

    Here, the accumulator parameter accumulates the result as we recurse, so once we reach the base case, we return the result directly.

    b. Head Recursion

    In head recursion, the recursive call occurs before any computation is done in the function. This typically requires maintaining the function’s stack until the recursion reaches the base case.

    Example: Head-recursive factorial function (similar to the earlier example):

    def factorial_head_recursive(n):
        if n == 0:
            return 1
        else:
            return n * factorial_head_recursive(n - 1)
    

    This is head recursion because the recursive call happens first before multiplying by nnn.


    5. Advantages and Disadvantages of Recursion

    Advantages:

    1. Simplicity: Recursive solutions are often simpler and easier to understand, especially for problems that have a recursive structure (e.g., factorial, Fibonacci, tree traversal).
    2. Natural Fit for Certain Problems: Problems like tree traversal, dynamic programming, and divide-and-conquer algorithms are naturally expressed with recursion.
    3. Code Clarity: Recursion can result in cleaner, shorter code compared to iterative solutions.

    Disadvantages:

    1. Performance: Recursive functions can be inefficient due to repeated function calls and the overhead of maintaining the call stack. This can result in stack overflow if the recursion goes too deep.
    2. Memory Usage: Recursive calls can consume a lot of memory because each recursive call adds a new frame to the call stack.
    3. Difficulty in Debugging: It can be harder to debug recursive functions due to the complexity of tracing the recursive calls.

    6. Recursion in Divide-and-Conquer Algorithms

    One common use of recursion is in divide-and-conquer algorithms, where the problem is divided into smaller subproblems, each solved recursively. Some well-known algorithms that use recursion include:

    • Merge Sort: Recursively divides an array into halves, sorts each half, and merges them back together.
    • Quick Sort: Recursively partitions the array and sorts the sub-arrays.
    • Binary Search: Recursively divides the search space to find the target value.

    Conclusion

    Recursive functions are a powerful tool in mathematics and computer science for solving problems by breaking them down into smaller subproblems. They are defined by a base case (the simplest condition that does not require further recursion) and a recursive case (which calls the function with modified parameters). While recursion can simplify problem-solving, it also has potential drawbacks, such as inefficiency and memory usage. Understanding when and how to use recursion effectively is key to leveraging its advantages in solving problems.

    ← Previous topic 27
    Inverse Functions
    Next topic 29 →
    Compositions

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    9mremaining
    1.5kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Discrete Structures