When is an identifier considered "declared"?


in the section on scopes it is stated that:

Functions and variables may be used, only (textually) after their declaration or definition.

This makes sense at first glance but I am unclear on it now:

int f(int x) {
    return g(x);

int g(int x) {
    return f(x);

My interpretation of this is that the specification says that an identifier may only be used if the locatable of declaration comes before the locatable of the use lexicographically. Here I say that locatable1 comes before locatable2 lexicographically if locatable1.getLine() < locatable2.getLine() or if locatable1.getLine() == locatable2.getLine() and locatable1.getColumn() < locatable2.getColumn().

In this interpretation the above use of g in f is illegitimate while the use of f in g is legitimate.

We follow C, where a declaration makes the declared identifiers available for use immediately after the end of the declarator.

Tis means that f is available from here:

int f(int x) {

and g similarly.

Thus this definition does not type. The background is that C was defined back when compilers used to only scan through the input once and not actually parse it into memory.

This is why you can declare methods. If you add int g(int); before line 1, you get the desired mutual recursion.

Hope this helps,

1 Like