Path: utzoo!attcan!uunet!peregrine!zardoz!dhw68k!arcturus!evil From: evil@arcturus.UUCP (Wade Guthrie) Newsgroups: comp.lang.c Subject: Re: Subroutine layout in C Summary: use static functions Message-ID: <3102@arcturus> Date: 22 Dec 88 17:03:24 GMT References: <2800002@uxg.cso.uiuc.edu> Organization: Rockwell International, Anaheim, CA Lines: 52 In article <2800002@uxg.cso.uiuc.edu>, phil@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 main() { global(); local(); } ----- file bar.c ------------- #include 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???)