functions within functions

Sean Eric Fagan sef at kithrup.COM
Sun Jan 27 20:20:18 AEST 1991


In article <11681 at pt.cs.cmu.edu> lindsay at gandalf.cs.cmu.edu (Donald Lindsay) writes:
>Some of us have used languages that allowed this, and didn't like it.

Somewhat agreed.  It *can* be useful at time, but having static functions
pretty much obliviates the needs, as far as I'm concerned.  (With nested
functions, you get lots and lots and *lots* of new scoping rules, which are
a real bitch to deal with, both as a user and as a compiler writer.  Ugh.)

>Personal opinion: the outer functions get unreadable.

Agreed.  And where do you allow their declaration?  Is

	void foo(int i) {
		int j;
		int strlen(int i) { return i/j; }
		int k;
		...
	}

legal, or do you only allow a window (a la pascal) for declaring functions?
Pascal had the advantage that it is a bit more readable as a result of the
restriction, but at the hindrance of the programmer's flow of thoughts.
(And, of course, the same can be said about C's requirement on variable
declaration; and I think the code is more readable, but disrupts
flow-of-thought.)

>Compiler writer's opinion: the calling convention now has to support
>uplevel addressing. 

Actually, that's not a terrible problem.  How to deal with nested call
frames is very well understood, as are the optimisations you can do on them.
But it's still a mess.

Note, incidenlty, that Metaware's HighC allows nested functions, and gcc 2.0
will also allow them.  Both compilers allow taking the address of a nested
function, but HighC added to the syntax of the language to do so, whereas
gcc just uses the same syntax (and notes what you're doing).

As for the original question:  the committee's job was to *standardize* C.
Very few C compilers allowed nested functions, and none of the books
describing the language "C" allowed for them.  It was not in the language.
My usual point for this type of thing is to bring up two things that ANSI
introduced to C:  prototypes, which most people agree are good, but still
have problems with%, and trigraphs, which almost nobody likes, including the
people who need them.

-----
% Given:
int foo(char);
int foo(i) char i; { return i; }

gcc (correctly) gives an error (unless -traditional is used, I believe);
most other compilers accept it (maybe warning about it at a certain level),
but not by default.
-----

-- 
Sean Eric Fagan  | "I made the universe, but please don't blame me for it;
sef at kithrup.COM  |  I had a bellyache at the time."
-----------------+           -- The Turtle (Stephen King, _It_)
Any opinions expressed are my own, and generally unpopular with others.



More information about the Comp.std.c mailing list