Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
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).

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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.

over 4 years ago · Santiago Trujillo Denunciar

0

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

int giveMeFive()
return 5;

and this construction

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

That is whether the programmer made a typo.

At present the compiler issues an error. But what to do if such a function definition will be allowed? This will make the program much less clear.

In fact it could be used like a simplified inline function

T function( parameter-list ) return expression;

However placing a semicolon between the function header and its body

T function( parameter-list ) ; return expression;

changes the symantic and makes the code unclear.

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

while( condition );

does not change the definition of the construction. In any case it is the while statement.

However relative to the function definition the situation is different.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda