16

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SLN
  • 4,772
  • 2
  • 38
  • 79
  • 1
    "I keep reading" where? – Tony Tannous Sep 19 '20 at 23:31
  • 10
    Would you care to quote the source of those blanket affirmations like "*statements alter the status of the machine*"? Consider for example `if(0) abort();` which *is* a C statement. Short answer is that `return` is a statement because that's what the language [defines it](https://en.cppreference.com/w/c/language/return) to be. – dxiv Sep 19 '20 at 23:32
  • 7
    This is nonsense. There are lots of statements that don't have side effects. – Nate Eldredge Sep 19 '20 at 23:32
  • 4
    You are over-thinking it. – selbie Sep 19 '20 at 23:32
  • 2
    On the other hand, you may consider that program flow is part of the status of the machine (its program counter, perhaps). `return` certainly alters that. – Nate Eldredge Sep 19 '20 at 23:34
  • @dxiv is correct - it's a statement because the standard defines it to be. Also, it behaves, syntactically, mostly like other statements. – einpoklum Sep 19 '20 at 23:43
  • 4
    Just playing 'devil's advocate' (or some such) here: but is `return` a statement or a keyword? For sure, `return;` (note the seventh character) **is** a statement. – Adrian Mole Sep 20 '20 at 00:00
  • 1
    @NateEldredge Indeed: [What is a null statement in C?](https://stackoverflow.com/q/40440400/10871073) – Adrian Mole Sep 20 '20 at 00:03

3 Answers3

12

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.

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 2
    Does C have a machine model which mandate a stack even exists? – einpoklum Sep 19 '20 at 23:41
  • 4
    @einpoklum: Yes. This answer says it changes the **call stack**, not the **hardware stack**. The semantics defined by the C standard for function calls—first in, last out—constitute the behavior of an abstract data structure called a stack. A C implementation must implement, in at least a limited way, some form of stack. – Eric Postpischil Sep 20 '20 at 00:00
  • 1
    @einpoklum Yes and no. It depends on what you mean by "stack". There must be some kind of first-in last-out structure for objects with automatic storage duration (non-`static` objects defined inside functions). But there's no requirement for this structure to be managed as a contiguous region of memory that grows and shrinks from one end. – Keith Thompson Sep 20 '20 at 00:52
10

statements alter the status of the machine

Except when they don’t. There are statements in C that have no side effects.

A statement is also a syntactic construct - it’s not about whether it has side effects or not, it’s about where it fits in the language grammar.

John Bode
  • 119,563
  • 19
  • 122
  • 198
5

When a program is running, the CPU needs to keep track of where it is in the code. This is done using a 'register' that is called, variously, a program counter, an instruction pointer, an address register or any one of a number of other, similar names.

The value in this, just like that in any other register or memory location, constitutes part of the "status of the machine." Furthermore, it is probably the most important 'status' with regards to running a program.

When your program executes a return statement, the value in this 'address register' is changed - to a value that corresponds to the piece of code immediately following the call to the function from which your are returning.

The return statement also (almost always) changes a number of other registers that comprise the status of the machine; for example, the stack pointer (if used) will be reset to its value before the call to the function was made.


Note: I have grossly oversimplified the CPU-level, run-time mechanics involved in calling (and returning from) a function here; however, the 'example' will hopefully illustrate the point that the return statement must affect the "status of the machine!"

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83