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?
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?
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.
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!
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.