Search code examples
programming-languages

When do formal parameters not hide external variables?


Preamble

So, I'm going through The C Programming Language and this quote struck me:

Automatic variables, including formal parameters, also hide external variables and functions of the same name.

The example:

int x;

// x inside of f is different from external f.
void f(double x){}

TL;DR

This strikes me as something which is necessarily true of all languages (and it dates back to Lambda Calc.), and yet it makes it into this book. Is there an example where the most local definition of a variable does not override a more global definition?


Solution

  • Its definitely not a necessary condition for a language. It just so happens that all the languages I can think of handle their scopes in this way. Why? Probably because that's how it has been done for so long and it makes the most sense, both for the compiler and the programmer (think about stacks).

    However, while I was in school, I did an experiment with an interpreted language in which symbols were put in a queue. As such, the most global scope overrode the local scopes. The language still worked and was fully functional. The only difference was that local scopes were overridden by global scopes. What this boils down to is just being careful about your naming in more global scopes.