Why are brackets required for a single sleeve?

We can do it

if(condition) doThis(); 

and this one

 while(condition) doThat(); 

but not this

 int giveMeFive() return 5; // Error: expected a '{' 

Why not?

I know that language grammar requires { and } to define functions. I ask about the rationale for the difference between conditional statements (which do not require brackets) and function definitions (which do).

+5
source share
3 answers

The reason, apparently, is mostly historical.

Prior to the 1989 ANSI C standard, C had no prototypes. Parameters of type int can be specified implicitly; parameters of types other than int should have been defined explicitly before { .

For example, where in modern C we could write:

 double sum(double x, double y) { return x + y; } 

in pre-ANSI (K & R) C we had to write:

 double sum(x, y) double x; double y; { return x + y; } 

The body of a function can begin with a local variable declaration:

 double sum(x, y) double x; double y; { double z; /* ... */ } 

Opening { was necessary to separate the definitions of parameters from the body of the function, and closing } was necessary to match the opening { .

(This syntax is still resolved, but is deprecated, in modern C, it is not resolved in C ++.)

When prototypes were added to the language, there were no particular reasons for excluding the definitions of the { and } functions.

The trivial answer is what language grammar requires. Function Definition Syntax:

Declarator declaration decla-list opt compound statement

where the compound operator consists of { followed by 0 or more declarations and operators followed by } . This is the only syntax production that requires a compound statement; there are many others that simply require approval.

Interestingly, in the predecessor language C, called B ( described here ), the syntax for defining a function is:

 name ( arguments ) statement 

The operator was usually a compound operator (a block separated by { and } ), but this was not required. B did not require or even allow the arguments and variables to indicate their types, so it was not necessary to have a specific syntax to separate the argument list in brackets from the function body. In the earliest C link I can find ( this one, since 1974 ), the syntax for defining a function has been changed to require a compound statement, probably to add adding parameter declarations.

+12
source

If allowed, there will be no difference between a function declaration:

 void a(); 

and defining a function for a function that does nothing:

 void a() ; 

This will add more problems than amplification.

+10
source

I think the problem is that it is difficult to distinguish this design

 int giveMeFive() return 5; 

and this design

 int giveMeFive() //; ^^^ return 5; 

That is, the programmer made a typo.

The compiler is currently reporting an error. But what if this function definition is allowed? This will make the program less understandable.

In fact, it can be used as a simplified built-in function.

 T function( parameter-list ) return expression; 

However, placing a semicolon between the function header and its body

 T function( parameter-list ) ; return expression; 

modifies symantic and makes code unclear.

The difference, for example, with the while statement, is that a semicolon after the while, like this

 while( condition ); 

does not change design definitions. In any case, this is a while statement.

However, compared to the definition of a function, the situation is different.

-2
source

All Articles