C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Functions
  5. Nested and Recursive Function

Nested and Recursive Function

C does not support nested function definitions, which means you cannot define a function inside another function.

  • However, nested function calls (i.e., calling a function inside another function) are allowed.

Example:

int multiply(int a, int b) {
    return a * b;
}

int square(int x) {
    return multiply(x, x);  // Nested function call
}
  • Here, the function square calls the function multiply inside its body — this is a nested function call.

A recursive function is a function that calls itself to solve a smaller subproblem.

  • It must have a base condition to terminate recursion and prevent infinite loops.

Example:

int factorial(int n) {
    if (n == 0)
        return 1;  // base case
    else
        return n * factorial(n - 1);  // recursive call
}
  • The factorial function calls itself with a decremented value of n until it reaches 0.

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *