cstatements

Why is the function return in C a statement?


An expression generates a value, statements alter the status of the machine, aka, side effects. However, I keep reading that function return is a statement. If I call a function that returns a void, how does that change any status of the machine? Or if I call a function that returns a non-void value, if I don't use it but just calling it how this changes any status?

I just don't get why the return is a statement?

Source: Concepts in Programming Languages. Cambridge: Cambridge University Press, 3.4.1 Statements and Expressions, p. 26


Solution

  • It changes the call stack and program counter. It puts the return value in a known place (depending on calling conventions)

    Even if you don’t use the return value, the compiler still needs to store it somewhere as it may be called from different compiler units that are unknown.