2

After calling return a statement, it was brought out to me in the comments:

return isn't a statement, it's the keyword that starts the return statement.

What is the difference between a statement and a keyword that starts a statement?

Community
  • 1
  • 1
IQAndreas
  • 8,060
  • 8
  • 39
  • 74
  • 5
    What's the difference between a sentence and a noun that starts a sentence? ;-) –  Apr 12 '14 at 18:01
  • @delnan Does that mean that `return` is a **keyword**, while `return true;` is a **statement**? – IQAndreas Apr 12 '14 at 18:02
  • @delnan Great explanation, it clarified things very well! I would be very pleased if you added it as a full answer (expanding on the point a tiny bit more) so I could accept it. – IQAndreas Apr 12 '14 at 18:04

3 Answers3

4

What's the difference between a sentence and a noun that starts a sentence? ;-)

return is a keyword, which means it's one of a few basic terms (tokens) of the language. They are privileged, each reserved for a special purpose and having special meaning (compare this with run of the mill identifiers/names).

A statement is (in broad terms - specific differ between languages) a higher-level unit of the language, akin to (a particular kind of) sentence in natural language. Statements include return 1+1; and foo(bar);, but generally not expressions like 1+1 or foo(bar).

Keywords often form part of statements (e.g. return introduces a return statement), but they never make a full statement on their own - even return; still needs a statement terminator.

  • Regarding the last sentence, would the semicolon in this case be the required **statement terminator**? – IQAndreas Apr 12 '14 at 18:13
  • 1
    @IQAndreas It is in my examples, where I use C-like syntax. Other languages have other statement terminators (newlines in Python) or statement *separators* (`begin stmt1; stmt2 end` in Pascal). Some languages don't have a concept of statements at all, actually. –  Apr 12 '14 at 18:17
2

A keyword (sometimes called a reserved word) is some word with a special meaning inside of a programming language. For example, in C, C++, and Java, int, void, and break are keywords, while in Python def is a keyword.

In an imperative programming language, a statement is a command that the program should execute. For example, the statement

x = y * 137;

means "evaluate the expression x = y * 137," while the statement

while (true) {
    x++;
}

means "continue to increment x forever."

Some keywords can be used in statements. For example, the statement

break;

means "break out of the current loop," while the statement

return true;

(which consists of the return and true keywords) means "exit the current function/method, yielding result true." These are called "break statements" and "return statements," respectively, and it's not incorrect to use the terms this way. However, break and return, by themselves, aren't statements. Notice that the syntax is

break;

with a semicolon and

return [opt-value];

with an optional value and semicolon. I think it's a bit of a stretch to say that it's incorrect to call return and break statements, since while it's technically incorrect to do so, everyone will know what you mean.

Note that some statements may consist of keywords, but not all keywords are statements. For example, you can't write

public;

or

volatile;

in any programming language that I know of.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • Very minor nitpick: Keywords and reserved words overlap and are often thrown together, but are distinct: Some words are reserved without giving them keyword status, they have no meaning and can't be used (e.g. `goto` in Java). On the other hand, contextual keywords (such as `yield` in C#) work as keywords in particular positions but are not reserved and can be used as identifiers anywhere identifiers are permitted. –  Apr 12 '14 at 18:16
1

The language lawyer in me points out that the C standard n1570 sayeth as follows.

S6.4.1 keywords includes:

return

S6.8 says:

A statement specifies an action to be performed.

S6.8.6. The return statement is defined to include the semicolon.

return expression(opt) ;

Answers the question, if not as interesting to read as some of the other answers.

david.pfx
  • 10,520
  • 3
  • 30
  • 63