jgh@mifnet.gec-mi-ate.co.uk (Jeremy Harris) (07/03/86)
In article <131@kaist.UUCP> jhhur@kaist.UUCP (Jin H. Hur) writes: >What is the meaning of 'static' function? Does it mean that the return value >is allocated a memory location rather than the top of C stack? If so, what is >the relevance of a memory location for a return value? No, it restricts the visibility of the function (scope) to the current file (module). This means 1) other modules can't use it as an entry point, 2) the name of the function may be reused in other modules of the same program, 3) the symbol table is smaller, less cluttered, etc (if you tell the compiler and/or loader to delete local-only symbols). Btw, lots of compilers return values from functions in register(s) rather than on the stack, for speed. One compiler I've seen (for a 68K) has a switch to return pointer values in A0 *as well as* D0. Interesting, no? Now, to light lotsa flames, :-) I think C's usage of 'static', as applied to functions, to mean 'don't export' is unfortunate. It is non-intuitive, as exemplified by the query above, and encourages the bad coding practice of exporting everything (since the default is to export). I would prefer the use of 'export' and 'import' as in Modula, with 'import' replacing 'extern'. The same arguments apply to variables as functions. This will break quite a lot of existing code, so must be availible as a compiler switch, to be used on all new code. Later, it will become the compiler defaualt, with a switch to turn it off. Ok, it's going to mean more typing during initial coding. But how much debugging time will it save you? *Anything* which encourages modular programming has got to be a good idea. How about it? Jeremy Harris ...!mcvax!ukc!hrc63!miduet!jgh (I don't speak for my employer) jgh@gec-mi-at.co.uk
lvs@ndm20 (07/07/86)
> jhhur@kaist.UUCP writes: >What is the meaning of 'static' function? Does it mean that the return value >is allocated a memory location rather than the top of C stack? If so, >what is the relevance of a memory location for a return value? The act of declaring a function static in no way affects the return value of the function. It merely restricts the scope of the function name to the file in which it lives. That is, the name of the function is NOT an external symbol and may not be referenced by functions in a seperately compiled unit. Note that this is the only way to restrict the scope of a function name. Without being defined 'static' all function names are external symbols. Hope this helps, Larry V. Streepy Jr. "Waiting is" Nathan D. Maier Consulting Engineers VOICE: (214)739-4741 Usenet: {seismo!c1east | cbosgd!sun | ihnp4}!convex!infoswx!ndm20!lvs CSNET: ndm20!lvs@smu ARPA: ndm20!lvs%smu@csnet-relay.ARPA