recursive function in c

#include <stdio.h>

void recursiveFunction(int n) {
    if (n > 0) {
        recursiveFunction(n - 1);
        printf("%d ", n);
    }
}

int main() {
    int x = 5;
    recursiveFunction(x);
    return 0;
}

Explanation:

  1. #include <stdio.h>: Including the standard input-output library for using functions like printf.

  2. void recursiveFunction(int n): Definition of a recursive function named recursiveFunction that takes an integer n as an argument and returns void (i.e., doesn't return a value).

  3. if (n > 0) { recursiveFunction(n - 1); printf("%d ", n); }: Inside the recursiveFunction, this if statement checks if the value of n is greater than 0. If true, it calls itself recursively with n - 1 and then prints the value of n.

  4. int main() { ... }: The main function where the program execution begins.

  5. int x = 5;: Declares an integer variable x and initializes it with the value 5.

  6. recursiveFunction(x);: Calls the recursiveFunction with the value of x (which is 5) as an argument.

When executed, this program will output:

1 2 3 4 5

This happens because the recursiveFunction is called with 5 as an argument, and it recursively calls itself with decremented values until n becomes 0. At each recursive call, it prints the value of n after the recursive call returns.