Neil Zanella <nz******@cs.mun.ca> writes:
I am curious as to why the designers of C did not include support for
Pascal-like nested procedures. I guess that it's because nested procedures
in C would not buy you much other than marginally cleaner namespaces, and
then again C has a static keyword for limiting the scope of functions to
a file, which is not quite the same, but seems enough for all practical
purposes. Anyone care to either agree or disagree?
I suspect part of the reason is that disallowing nested procedures
makes the compiler's job easier, something that was probably more
important when the language was being designed than it is now. The
problem is references to variables in enclosing procedures. For
example (in hypothetical C syntax):
void outer(void)
{
int outer_var = 1;
void inner(void)
{
int inner_var = outer_var;
}
}
Assuming a stack implementation, the reference to outer_var in inner()
requires digging some variable distance down the stack, depending on
the current state of the call stack. (Imagine that inner() is
recursive.)
I think Algol supports nested procedures so there was precedent for
the idea, but it wasn't thought to be worth the extra effort.
--
Keith Thompson (The_Other_Keith)
ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"