0
include <stdio.h>
int sum = 1;
int fact(int num){
    if (num == 0){
        return 1;
    }
    fact(num -1);
    sum *=num;
}

int main()
{
    int num = 5;  
    printf("%d ", fact(num));
    return 0;
}

The output of the above program prints 120 though I don't have a return from the recursive condition. I do have a basic understanding of how recursive calls creates a function on the stack. Could someone explain to me how this program prints output without any return?

Update: The below code return value of 1 as expected.

#include <stdio.h>
int sum = 1;
int fact(int num){
    if (num == 0){
        return 1;
    }
    num * fact(num -1);
}

int main()
{
    int num = 5;  
    printf("%d ", fact(num));
    return 0;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
nullbyte91
  • 161
  • 2
  • 9

0 Answers0