Josep Portella

A vestige of the classic C language

October 2011
Translated: October 2013

© 2013 Josep Portella Florit
This work is licensed under a
Attribution-NoDerivs 3.0 Creative Commons license.

On most of the control structures of certain programming languages with C-based syntax, e.g. PHP or JavaScript, there is the possibility to group sentences between curly braces ({}) or else put a single sentence without braces. However, this option is not available on function declarations, just like on the C language; the curly braces are obligatory even on single sentence functions.

I guess those languages' designers didn’t ask themselves whether they had to let the braces be omitted on single sentence functions, but they simply saw it was done that way in C and they didn’t think twice about it.

Technically, there would be no problem in letting the braces be omitted on single sentence function declarations in PHP and JavaScript. In fact, you can do it in the new JavaScript 1.8 standard. On the other hand, you can’t do it in C because of compatibility with classic or K&R C, the language as defined by the first edition of Kernighan and Ritchie’s The C Programming Language.

If we compare the declaration of a function that returns the square of an integer in standard C (ANSI C)

int
square (int x)
{
  return x * x;
}

with the same function in classic C

int
square (x)
     int x;
{
  return x * x;
}

we can see that the parameter type declaration has moved, and that in classic C the curly braces are needed to distinguish a parameter type declaration from a sentence. In standard C it isn’t done this way, so if it wasn’t needed to be compatible with classic function declaration style, then it would be possible to omit the curly braces in single sentence functions.