Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Nested functions ?

4 views
Skip to first unread message

Igor Boukanov

unread,
Oct 16, 1996, 3:00:00 AM10/16/96
to

After some work with gcc I realized that nested functions are very useful
and powerful. Unfortunately it does not support them only for C but
hopefully C++ case will be done in a new version. So why don't have them
in the standard?
The implementation in almost any compiler can be done without
problems, just take gcc source code...

[ mod note: This ought to be in the FAQ list. -sdc ]

--
Regards, Igor Boukanov.
igor.b...@fi.uib.no
http://www.fi.uib.no/~boukanov/


[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++...@ncar.ucar.edu ]

Steve Clamage

unread,
Oct 16, 1996, 3:00:00 AM10/16/96
to

In article o...@toralf.uib.no, bouk...@sentef1.fi.uib.no (Igor Boukanov) writes:
>After some work with gcc I realized that nested functions are very useful
>and powerful. Unfortunately it does not support them only for C but
>hopefully C++ case will be done in a new version. So why don't have them
>in the standard?
>The implementation in almost any compiler can be done without
>problems, just take gcc source code...

A variety of languages have nested functions. Definition and implementation
are not the issue. The better question is why Standard C doesn't have nested
functions. I'm sure the reason Ritchie didn't put nested functions into
C was to keep function calling as simple as possible. C was supposed to be
a small, fast language, and a language that supports nested functions fully
(as in Algol or Pascal, for example) requires extra overhead in the function
call mechanism. (If you give up some features of nested functions, you
can avoid overhead in calls not involving nested functions. I believe
gcc makes that compromise.) Why were nested functions not added to Standard
C? You'd have to ask the C Committee members. I'm sure everyone on the
C Committee was familiar with the concept, so evidently not enough people
felt they were an important addition to C.

If C had nested functions, C++ would have them too, for compatibility. So
now the question becomes, "Why were nested functions not added to C++?"

Nested functions serve three purposes, and I'll address each of them as
applied to C++.

1. Scope control. It's handy to be able to write a little function to
perform a specialized action, and restrict its scope to one small area
of the program. You don't have to worry about name collisions, or exporting
interfaces you don't wish to document or maintain. C++ has many methods
of scope control already: file-scope static functions as in C; class member
functions, ordinary or static; namespaces. IMHO nested functions provide no
additional utility in scope control.

2. Implicit access to outer-scope variables (shorter parameter lists).
Although some people cite this as an advantage of nested functions, others
cite it as a disadvantage. When a name might come from any of many different
visible scopes, programs are hard to read and maintain. Even if you consider
this feature an advantage, you can get the same effect in C++ by putting
those variables into a struct or class, and passing a single reference to
the called subfunction. The code that gets generated is remarkably like
the code that gets generated if you have nested functions. By putting
selected variables in a structure, you emphasize that they have a
relationship. Bottom line: There is no efficiency issue, and it is far from
clear that nested functions offer a net improvement in program quality
(from this standpoint).

3. Lexical closure. You can pass a pointer to a nested function which
inherits a static (lexical) scope, and gets called from a different
environment. (This is a form of ecapsulation.) You can't simulate this
property of nested functions conveniently in C++, so maybe this would be
a reason to add nested classes to C++. But that assumes that the procedural
model is the best model for the program. If you adopt an OO model instead,
you get a slightly different program organization, and the static scope
inherited by a nested function becomes class data. This kind of program
organization C++ supports very well; languages that have nested functions
usually do not support OO programming.

In the end, it seems to me that nested functions do not solve any
programming problem for which C++ does not already have a solution at
least as good.

A proposal to add nested functions to C++ should show an important
class of programming problems solved by nested functions which are
inconvenient to solve otherwise. (Perhaps there are such problems, and
I simply haven't seen them.)
---
Steve Clamage, stephen...@eng.sun.com

0 new messages