🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.amiga.tech
Article #199

Re: Can you nest subroutines in C?

#199
From: Deven T. Corzine
Date: Tue, 27 Jun 1989 08:13
56 lines
3526 bytes
In article <4470@crash.cts.com> wade@pnet01.cts.com (Wade Bickel) writes:

>    Recently I switched from Benchmark Modula-2 to Lattice C.  While
>writing a piece of code I discovered, much to my dismay, that I could
>not express nested subroutines without choking the compiler.  Perhaps
>someone can enlighten me as to how to do this.

Sorry, you lose.  This is the one thing which keeps C from being a
strictly block-structured language -- function definitions can not
contain other function definitions.  Yet there are workarounds.

>    The nested subroutine InitScreenBuffer() is nested inside of
>InitMB().  The advantage to doing this is that it avoids either the
>declaration of w,h, and d (width,height and depth respectively) as
>globals or passing them as parameters to InitScreenBuffer() in which
>case they would appear three times on the stack (18 bytes vs 6).

One midpoint approach is to put the data values in a structure, and
pass a pointer to the structure.  This still involves passing an extra
parameter, but avoids duplicating the data unnecessarily.  Another
approach is to make a function with its "nested" functions a separate
module, with variables global to the module, but not defined external
to the module.  I can't remember offhand how to declare such; maybe
"static int x" outside of function defintions would do it.  (need to
override the otherwise assumed extern)  Anyone have K&R's book handy?
(Note that this method is NOT reentrant, but passing pointers (held in
local variables) to dynamically allocated memory IS reentrant.)

>    In more complex algorithms involving trees of data I have found
>the use of these psuedo globals very handy.  Also, anytime a segment
>of code needs a large space of temporary data which will be visable
>to several routines, nesting is an easy, memory efficient method of
>allocating it.  I often bottle up my entire program in this manner to
>avoid declaration of any global variables at all.  While I have not
>written any, it seems this should be an easy way to create reentrant
>code.

Although slightly more work, using AllocMem (or malloc) and passing
arounds pointers (to a structure if many discrete variables, like a
bunch of integers -- or maybe an array if appropriate) is equally
memory-efficient, and reentrant.

>    I hope there is a way to create nested functions in C.  In
>general I find I like C.  Especially the flexability of conditionals
>afforded in pointer manipulations, which is much more powerful than
>that allowed in Modula-2.

Yes, C is a powerful and flexible language, even if somewhat terse.
(I know that *I* like it!)

Deven
--
shadow@[128.113.10.2]   <shadow@pawl.rpi.edu> Deven T. Corzine (518) 272-5847
shadow@[128.113.10.201] <shadow@acm.rpi.edu>  2346 15th St.    Pi-Rho America
deven@rpitsmts.bitnet   <userfxb6@rpitsmts>   Troy, NY 12180-2306  <<tionen>>
"Simple things should be simple and complex things should be possible." - A.K.
Technical Details
Message-ID: <SHADOW.89Jun27041334@daniel.pawl.rpi.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!uwvax!dogie.macc.wisc.edu!csd4.milw.wisc.edu!leah!rpi!pawl!shadow
References: <4470@crash.cts.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!uwvax!dogie.macc.wisc.edu!csd4.milw.wisc.edu!leah!rpi!pawl!shadow
From: shadow@pawl.rpi.edu (Deven T. Corzine)
Newsgroups: comp.sys.amiga.tech
Subject: Re: Can you nest subroutines in C?
Message-ID: <SHADOW.89Jun27041334@daniel.pawl.rpi.edu>
Date: 27 Jun 89 08:13:35 GMT
References: <4470@crash.cts.com>
Sender: usenet@rpi.edu
Organization: Rensselaer Polytechnic Institute, Troy, NY
Lines: 56
In-reply-to: wade@pnet01.cts.com's message of 26 Jun 89 12:16:31 GMT