Skip to main content

Recursive Functions in C

 Lecture Notes: Recursive Functions in C

Introduction to Recursive Functions:

Recursive functions are functions that call themselves directly or indirectly to solve a problem by dividing it into smaller instances of the same problem. They are a powerful programming technique used to solve problems that can be broken down into simpler subproblems.

Definition:

A recursive function is a function that calls itself either directly or indirectly during its execution.

Types of Recursive Functions:

  1. Direct Recursion: In direct recursion, a function calls itself directly.

    c
    // Example of direct recursion void countdown(int n) { if (n > 0) { printf("%d ", n); countdown(n - 1); // Recursive call } }
  2. Indirect Recursion: In indirect recursion, a function calls another function, which in turn calls the first function, creating a cycle of function calls.

    c
    // Example of indirect recursion void func1(int n); void func2(int n) { if (n > 0) { printf("%d ", n); func1(n / 2); // Recursive call to func1 } } void func1(int n) { if (n > 1) { printf("%d ", n); func2(n - 1); // Recursive call to func2 } }

Explanation with Examples:

  1. Direct Recursion:

    • Factorial Function:

      The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.

      c
      int factorial(int n) { if (n == 0 || n == 1) return 1; else return n * factorial(n - 1); }

      Explanation: The function factorial() calculates the factorial of a number by calling itself with a smaller argument (n - 1) until it reaches the base case (n == 0 or n == 1), at which point it returns 1.

  2. Indirect Recursion:

    • Odd and Even Numbers:

      This example demonstrates indirect recursion by printing odd and even numbers in a given range using two mutually recursive functions.

      c
      void printOdd(int n); void printEven(int n) { if (n > 0) { printf("%d ", n); printOdd(n - 1); // Recursive call to printOdd } } void printOdd(int n) { if (n > 0) { printf("%d ", n); printEven(n - 1); // Recursive call to printEven } }

      Explanation: The functions printOdd() and printEven() call each other alternatively, printing odd and even numbers respectively until the base case (n == 0) is reached.


Conclusion:

Recursive functions provide an elegant solution for solving problems that can be divided into smaller subproblems. However, excessive recursion can lead to stack overflow errors, and recursive functions may not always be the most efficient solution. It is essential to understand the principles of recursion and when to use it appropriately in programming.

Comments

Popular posts from this blog

Header Files and their Examples in C

  What Is A Header File In C? A header file in C language is a text file containing definitions of functions, variables, and macros that are shared among multiple source files. It provides an interface to the variety of functions and data structures that can be used by other parts of the program. Essentially, a header file serves as a contract that specifies what features/ operations are available to the rest of the program without exposing the implementation details. Standard Header File In C & Their Uses In C, there are many standard header files, each of which has a unique collection of utilities and methods. Naturally, there are a few standard C header files that are most typically used in applications/ C code.  We have compiled a detailed explanation of all 25 standard header files in C. The <stdio.h> Header File In C The name stands for  standard input/output header , and it contains functions for standard input and output operations such as...

Introduction to Pointers in C

  Introduction to  Pointers Introduction: In C programming, a pointer is a special variable that stores the memory address of another variable. Pointers are powerful tools that allow direct manipulation of memory and enable dynamic memory allocation. They play a crucial role in tasks such as dynamic memory allocation, accessing hardware, implementing data structures, and optimizing code performance. In simple terms pointers are the variables which store addresses of another variable. It can also store addresses of functions, arrays, structures and other pointers along with primitive data type variables. But the question arises “ why do we need pointers? “. Let’s find out the answer for the above question. Declaration of pointers Declaration of pointer variables is almost same as declaring normal variable, only difference is that we add ‘ * (asterisk) ‘ sign before the pointer variable. Syntax data_type *pointer_variable_name; E.g. int *num;  // pointer variable which can...