Subroutine layout in C
Wade Guthrie
evil at arcturus.UUCP
Fri Dec 23 03:03:24 AEST 1988
In article <2800002 at uxg.cso.uiuc.edu>, phil at uxg.cso.uiuc.edu writes:
> I want to write a subroutine in C, called S. I want S to be known outside.
> I also want to have two subroutines X and Y to be known ONLY to S (not known
> outside of S). Either can be called by S, and each calls the other in a
> recursive way. I also need to share several variables entirely within
> this context (shared between S, X, Y). They can be static. There will
> only be 1 instance of S (and therefore also of X and Y, but that should
> be hidden). Main program M should be able to call S, but any references
> to X and Y will not be resolved by the module S.
>
In a file, which is separately compiled before being linked to the rest
of your code, put S(), X(), and Y(). Declare X() and Y() to be static
which, in addition to causing variables to maintain their values between
calls, causes symbols to be unknown outside that file. As an example:
----- file foo.c -------------
#include <stdio.h>
main()
{
global();
local();
}
----- file bar.c -------------
#include <stdio.h>
int
global()
{
puts("entered global");
local();
puts("leaving global");
}
static int
local() { puts(" calling local"); }
--------------------------
try compiling it, and you'll see that main cannot access local. It works
if you either 1) remove the call to local in main, or 2) remove the static
declaration of local.
Wade Guthrie
Rockwell International
Anaheim, CA
(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)
More information about the Comp.lang.c
mailing list