Function declarations (was: MSC v5.1 Bug???)

Steve Uitti suitti at haddock.ima.isc.com
Tue Aug 23 02:07:28 AEST 1988


In article <5680 at rpp386.UUCP> jfh at rpp386.UUCP (The Beach Bum) writes:
>In article <11879 at iuvax.cs.indiana.edu> bobmon at iuvax.UUCP (RAMontante) writes:
>>Craig Dawson asks about a program in which two routines try to call a
>>third, somewhat as follows:
>>
>>x() { static z(); z(); }
>>y() { static z(); z(); }
>>
>>static z() { ; }
>>
>>(MSC apparently chokes; BTW, TurboC accepts it.)  My question is... Why
>>declare z() inside the functions x() and y()?

	Supporting other peoples code is more difficult than
supporting your own.  A thirty file program where everything (that can
be) is declared static is much easier to support than one that
doesn't.  "ctags", "grep", and cxref only do so much for you.
	The tighter the scope of each variable and function, the
better.  I generally try to declare the functions before all uses:
static z(a) int a; { ; }
static y() { z(5); }
static x() { y(); z(); }
main() { x(); }
	Kind of reminds you of Pascal (except that you can tell where
"main" starts).  The complexity of a program at any point seems to be
tied exponentialy to the number of variables and functions available
to it at that point.
	Unfortunately, the compilers that I've used that support
prototypes don't "automatically generate" them using this style.  If I
want prototypes for the above, I need:
static z(a) int a; { ; }
#ifdef PROTOTYPES_SUPPORTED
static z(int a); /* prototype for z() */
#endif /* PROTOTYPES_SUPPORTED */
static y() { z(5); }
static x() { y(); z(); }
main() { x(); }
	Which is too bad, since it really has all the info it could
possibly want, and I'll probably just screw up the prototype (since I can).

	On PDP-11's, the linker (ld) can run out of symbol space for
the symbol table if you don't do this.  Generally, the larger editors
that have been ported to this machine got hundreds of routines
declared "static", just to allow compilation.  This wasn't "bad".  It
made the products easier to maintain.

	Another favorite is this:
y() { z(); }
static z() { ; }
	This often yields something like "redeclaration of z".  The
first reference defaults to something like "extern int z();" for a
declaration.  I've always thought that this was a language feature.
It encourages better behaviour without requiring it.  Of course the
following is fine:
static z() { ; }
y() { z(); }

	Stephen



More information about the Comp.lang.c mailing list