1

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?

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166

3 Answers3

1

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.

Chad La Guardia
  • 5,088
  • 4
  • 24
  • 35
  • Ah, the language was called h-- or something like that...it was written by a grad student, its not a *real* language. I was the one who modified it to use a queue. – Chad La Guardia Sep 09 '11 at 23:24
0

It was a struggle, but I found a very, very weak example of a global variable overriding a local one. It doesn't really count, but it's all I could find!

I'm sure that explanation was included in K&R because they didn't want to assume prior programming experience. Local scope overriding global scope is second nature to most of us, but a fresh mind wouldn't have that knowledge. Stating it explicitly causes you to think about why it might be true, and that leads to enlightenment! :)

Community
  • 1
  • 1
mwcz
  • 8,949
  • 10
  • 42
  • 63
0

In languages with dynamic scoping, the inner x wouldn't hide the outer x, it would modify the outer x. See the example on the wikipedia page. Languages with dynamic scoping, particularly dialects of Lisp, were more common when K&R was written. Dynamic scoping interacts poorly with any sort of type system, though, even a type system as loosely enforced as C's.

Ryan Culpepper
  • 10,495
  • 4
  • 31
  • 30
  • But the parameter list, if I am not mistaken, is always sacrosanct in Lisp `(defun foo(bar)(print bar))(defparameter bar 1)(foo 2)` prints 2. Perl, of course, does not have a parameter list, so it can do whatever it wants. – cwallenpoole Sep 09 '11 at 21:50
  • @cwallenpool, I don't think the parameter list is sacrosact; rather, anything that would introduce a *dynamic* binding between a variable and its nearest enclosing parameter list is also *lexically* obvious (eg, a `let` form). OTOH, if you have a nested function defition (using `flet` or `labels`, IIRC) whose body refers to a variable from an outer parameter list, I expect you could still observe dynamic scoping. – Ryan Culpepper Sep 09 '11 at 22:36