1

why the following code is returning the output even if I am not returning the output of variable a to the previous function.

int fact(int n)
{
    int a;
    if (n <= 1) 
        return 1;
    else
        a = n*fact(n-1);
}
int main()
{
    int c=fact(5);
    printf("%d",c);
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 3
    Welcome to the wonderful world of [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). In short, if you declare a function to return a value, you *must* return a value, anything else is wrong. – Some programmer dude Jul 24 '18 at 05:32

1 Answers1

2

Quoting C11, chapter §6.9.1

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

So, your program exhibits undefined behavior.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261