Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do one liner functions require braces?

We can do this

if(condition)
    doThis();

and this

while(condition)
    doThat();

but not this

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

Why not?

I'm aware that the language grammar requires { and } on function definitions. I'm asking about the rationale for the difference between conditional statements (which don't require braces) and function definitions (which do).

like image 768
cambunctious Avatar asked Aug 05 '15 17:08

cambunctious


People also ask

Why do we use block of statements with braces?

Braces are used around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

DO IF statements need curly braces?

If the true or false clause of an if statement has only one statement, you do not need to use braces (also called "curly brackets"). This braceless style is dangerous, and most style guides recommend always using them.

Do you need brackets for if statements C++?

In C, C++ and Java, if you add another statement to the then clause of an if-then: if (condition) statement1();


2 Answers

The reason seems to be mostly historical.

Prior to the 1989 ANSI C standard, C did not have prototypes. Parameters of type int could be defined implicitly; parameters of types other than int had to be defined explicitly, before the opening {.

For example, where in modern C we might 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 the function might begin with a declaration of a local variable:

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

The opening { was needed to separate the parameter definitions from the body of the function, and the closing } was needed to match the opening {.

(This syntax is still allowed, but obsolescent, in modern C; it's not allowed in C++.)

When prototypes were added to the language, there was no particular reason to permit omitting the { and } in function definitions.

The trivial answer is that that's what the language grammar requires. The syntax for a function-definition is:

declaration-specifiers declarator declaration-listopt compound-statement

where a compound-statement consists of {, followed by 0 or more declarations and statements, followed by }. This is the only syntax production that requires a compound-statement; there are plenty of others that merely require a statement.

Interestingly, in C's predecessor language, called B (documented here), the syntax of a function definition was:

name ( arguments ) statement

The statement was usually a compound statement (a block delimited by { and }), but it wasn't required to be. B did not require, or even allow, arguments and variables to have their types specified, so there was no need to have a particular syntax to separate the parenthesized argument list from the body of the function. In the earliest C reference I can find (this one, from 1974), the syntax of a function definition had been changed to require a compound statement, probably to accommodate the addition of parameter declarations.

like image 122
Keith Thompson Avatar answered Oct 12 '22 23:10

Keith Thompson


If it was allowed, there would be no difference between a function declaration:

void a();

and a function definition for a function doing nothing:

void a()
  ;

It would add more problems than gain for sure.

like image 20
Martin G Avatar answered Oct 13 '22 01:10

Martin G