Why can't you create an expression function 1 without curly braces?

So, in most programming languages, if you use a loop or if, you can do this without curly braces if it has only one statement, for example:

if (true)
    //Single statement;

for (int i = 0; i < 10; i++)
    //Single Statement

while (true)
    //Single statement

However, it does not work for functions, for example:

void myFunction()
    //Single Statement

So my question is, why doesn't it work for functions?

+4
source share
8 answers

C ++ requires it to disambiguate some constructs:

void Foo::bar() const int i = 5;

Now constowned baror i?

+8
source

Because language grammar forbids you to do this.

A Java grammar defines a method as follows:

MethodDeclaration:
    MethodHeader MethodBody

Body method like:

MethodBody:
    Block 
    ;

(. ),

Block:
    { BlockStatementsopt }

.

if :

IfThenStatement:
    if ( Expression ) Statement

), .

? .

: http://docs.oracle.com/javase/specs/jls/se7/html/index.html

+5

, (Python? , , :))), .

, , , , :

namespace Example
    class Foo : public Bar
        public: std::string myMethod()
            return "Oh noes!";

? , ?

.

, . , , ,

namespace Example class Foo : public Bar public: std::string myMethod() return "Oh noes!";

, . ,

namespace Example { class Foo : public Bar { public: std::string myMethod() {return "Oh noes!";}}}

, , .

( ? , ), : . - . , ", ". , . , , , .. , , C- , .

,

namespace Example class Foo : public Bar public : std::string myMethod() return "Oh noes!";
                            ^                   ^    ^^

. , , , , ":". - , , , . , , "::", ":".

, :

namespace Example class Foo : public Bar public : std::string myMethod() return "Oh noes!";
                              ^^^^^^     ^^^^^^

, . , , ":", !

, , 2 : public ':'. , . , . , , - . Nth , "" , "public" :

namespace Example class Foo : public ::Bar public : std::string myMethod() return "Oh noes!";
                              ^^^^^^^^     ^^^^^^^^

, Bar ::Bar. , "public " . , " ", , , - "public" ":" .

: , . "" , , . , oh-so-long .

, , . , - , , .

++, , ';' .h? }? 30 300 . , ';' '{}' , 30 300 , - ! , .

: , , -/ , :

  • (, , )
  • ( !)

, .

++ , . Java C, , /, , . C, #include . Java , , , , ++..

, , , , , .. ( ) . , LISP. , ( , - VisualBasic " if" yuck)

, , Python, ? / .. intendation. . , , //etc , , . .

+4

() , . . , , , .

, ?

+1

, , , ? # , Java.

, , , , .

0

.

C, ISO/IEC 9899-1999:

6.9.1 Function definitions
Syntax
1     function-definition:
          declaration-specifiers declarator declaration-listopt compound-statement

- ,

compound-statement:
    { block-item-listopt }

. .

An if, while statement .

 (6.8.5) iteration-statement:
             while ( expression ) statement

.

statement:
    labeled-statement
    compound-statement
    expression-statement
    selection-statement
    iteration-statement
    jump-statement

compound-statement .

0

, # -. . Lambda #. # 3.0 .

0

++ , , . , , :

int foobar()
    try {
      return 1;
    }
    catch (...){return 0;}
0

All Articles