nested procedures and the C language

N

Neil Zanella

Hello,

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?

Regards,

Neil
 
G

Grumble

Neil said:
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?

This is Frequently Asked Question number 20.24

http://www.eskimo.com/~scs/C-faq/q20.24.html
 
K

Keith Thompson

Neil Zanella said:
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.
 
D

Derk Gwen

# 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?

Because Fortran didn't. C is mixture of Fortran and Jovial and a sprinkling
of Algol 68. The stack display and static link were well understood.
 
C

CBFalconer

Keith said:
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.

In Pascal, which was originally designed to have a single source
file and no separate compilation facilities, the feature was very
attractive for proper localization. In C, with the separate
compilations, it is much less necessary inasmuch as there are
other methods of limiting scope.
 
O

osmium

Neil said:
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 see it (The C way) as the natural view of the world as seem by an assembly
language programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top