-1

in the call stacks short there is a code when doug explains that the function returns (1) i want to know how the c knows or the computer knows the difference between returning (1) mean the program crash and return (1) and the program return the value to a variable or a function and continues. here's the code :

int fact(int n) { if (n == 1) return 1; else return n * fact( n - 1); }

thanks in advance for your help

1 Answer 1

0

I think you're conflating a couple of different concepts here. The standard practice is this. When a program returns a value to the operating system upon completion or termination, it will return a 0 to indicate successful or normal completion. When a program returns a non-zero, it usually means that the program itself detected an error condition, such as bad input data, and the number gives an indication of what happened. But understand that, in either case, it's still a normal termination of the program. This is not an outright crash, which would be handled by the OS itself.

Now, in a function, a return statement with a return value is returning a calculated value back to the calling function or main. This has nothing to do with indicating a crash or adverse condition. In the function you gave as an example, the function is calculating one of two values, depending on the input value, n, that was passed to the function. In fact, this is a recursive function that calls itself until n=1. (It's a factorial function.)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • I think my mind was wrapped up but i got it, thanks alot
    – ox0o
    Commented Sep 13, 2019 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .