Is there any benefit from declaring variables at the beginning of a function?

Viewed 307

I know that pre C99 compilers needed variables to be declared at the beginning of a function to calculate stack size. Then the requirement was lifted. Nowadays, besides backwards compability, is there any benefit from still sticking to that scheme, or maybe is declaring variables only when they are needed and where they are needed better? (for instance in an if statement, where one branch requires a variable but second one doesn't)

2 Answers

The answer is no. There is no compilation or other computing benefit from declaring identifiers at the beginning of a function.

Good modern compilers analyze where values are used in code, so the locations of declarations are irrelevant as long as they do not affect semantics (such as moving a declaration into a compound statement, reducing its scope).

In some cases, there may be benefit from telling a reader what you are going to do at the beginning of a function or block. Generally, it is beneficial to declare identifiers just where they are needed, as this tends to reduce the number of things a reader has to think about at one time. However, if there is some pattern or rhyme and reason to the algorithm a function is going to perform, then showing some aspects of that at the beginning can help the reader comprehend it.

The C language has permitted declaration of variables in any statement block from at least 1978 as described in The C Programming Language Section 4.8 Block Structure. This is also described in Where you can and cannot declare new variables in C?.

More recent revisions, in addition to permitting declarations at the beginning of any block, allow declarations to be placed later in a block but does not permit forward references. The size of arrays can also be dynamically defined and are not restricted to constant expressions.

Whether there is any benefit opens the question of to whom. I can think of reasons where there is benefit:

  • Simpler functions may not benefit from the additional design effort of structuring of variable declarations,
  • Functions with locally scoped variables can silently hide declarations at a higher level,
  • Automatic variable declarations that contain dynamic sizing requiring checking before being allocated,
  • Readers of the code can see the identifiers referenced in a function in one location - often with comments that describe behaviour, and,
  • Code translated from other languages without this feature looks more like the original.

Conversely, I can think of several reasons why this is not beneficial:

  • A naïve compiler can use this information to improve stack frame allocation without static analysis (particularly with large variables),
  • A mechanical code generator can avoid name space clashes by declaring variables within a smaller scope without checking, and,
  • Long functions benefit from local scoping by avoiding potential use of values not valid throughout a function body.
Related