-3

in C++, there is no such thing as an assignment statement or function-call statement.

An assignment is an expression; a function-call is an expression; this is coming straight from Bjarne Stroustrup in his book "The C++ Programming Language".

I know an expression computes a value, which has me wondering if this applies to void functions, since they don't return a value.

I'd like to know if functions with a return type of void still count as expressions, and if so, why?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
j_burks
  • 93
  • 5
  • 2
    I went ahead and reduced the unrelated chatter from your question, and structured things a bit. – Marcus Müller Aug 26 '16 at 08:56
  • 1
    C++ standard: *1 An expression is a sequence of operators and operands that specifies computation of a value, **or** that designates an object or a function, **or** that generates side effects, **or** that performs a combination thereof* (emphasis by me) notice that an expression not necessarily computes a value; certain expressions do, however. – Marcus Müller Aug 26 '16 at 09:00
  • Possible duplicate of [Explaining the difference between a statement and an expression in c++](http://stackoverflow.com/questions/27600153/explaining-the-difference-between-a-statement-and-an-expression-in-c) – Marcus Müller Aug 26 '16 at 09:09
  • Just take your expression and add a semicolon - now you have a statement. – Bo Persson Aug 26 '16 at 09:11
  • Interesting. Thank you, Marcus. I had attempted to search with "C++ Standard" and "C++ ISO Standard" in the search (in Google, at least) and still couldn't find the answer to this. I'm going to have to find a book or website that lays out the Standard definitions like that, as it would go a long way toward answering questions that will crop up over time. – j_burks Aug 26 '16 at 09:21

4 Answers4

2

C++14 standard:

§5 Expressions:

1 An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects

So the "main" purpose/scope of an expression is to specify a computation, not to compute a value. Some computations may result in a value and some may have side effects.

In addition to this (or actually first of all), "expressions" and "statements" are used in defining the grammar of C and C++. It would be a syntactically impossible to make functions that don't return a value not an "expression". And adding that distinction at a semantic level would be an unnecessary overly-complication.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • Wow. The reference manual I have is outdated and only around 50 pages. That PDF is like a Stephen King novel and seems to go out of its way to unnecessarily complicate everything, rather than explain concepts in a clear manner. – j_burks Aug 26 '16 at 23:49
  • @j_burks that pdf is not a manual. It's the official C++ standard. It's not meant to explain things to novices. It's the document that defines in a formal, complete and unambiguous way what C++ is. That is the reference the C++ compilers and standard libraries are based on. – bolov Aug 27 '16 at 14:45
1

Yes functions returning no value (declared as returning void) still counts as expressions when you call them. That limits their use in other expressions though, for example such calls can not be on either side of an assignment.

As for "why"? Well, a function call is a function call is a function call. Adding special rules for functions that don't return a value would make the language design much more complicated. C++ already have enough special rules and exceptions.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Yes, void function call is also an expression. The definition in C++ Standard says:

An expression is a sequence of operators and operands that specifies a computation. An expression can result in a value and can cause side effects.

A function call is a postfix expression followed by parentheses containing a possibly empty, comma-separated list of expressions which constitute the arguments to the function.

Also in MSDN C++ Language Reference:

A postfix-expression followed by the function-call operator, ( ), specifies a function call.

mtszkw
  • 2,717
  • 2
  • 17
  • 31
0

Function Call = expression-statement… even functions of type void?

no. But that's because a function call is this:

do_stuff()

and that's an expression_opt. It's an expression, not a statement. You can use this expression in compound expressions, but it's not a statement by language logic.

You can quickly convert that expression_opt to a expression-statement by giving it a semicolon:

do_stuff();

is now a full statement.

The difference becomes clear if you think about something like

if(good_thing() || do_stuff())
{
    ....
}

do_stuff() and good_thing() are expressions, which can/will be evaluated. Semicolons after () would break that if clause.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
  • But if either do_stuff() or good_thing() have a return type of void, that won't even compile. I thought I had figured out what an expression-statement was by separating it from other statements (like declaration). For example, you cannot assign a declaration statement to an lvalue, like int x = int y = 5; But even that doesn't do it for an explanation. This is why I got so confused about expressions not evaluating to a value, because for years this is what I believed an "expression" was: an operation that returns a single value. Now I must "unlearn" what I have learned. – j_burks Aug 27 '16 at 02:12