As in:
Since 'bar' is in the global scope anyway (after the definition), and you can't have local function definitions, what is the point of allowing local function declarations?Code:void foo (void) { void bar(void); bar(); } void bar (void) { }
As in:
Since 'bar' is in the global scope anyway (after the definition), and you can't have local function definitions, what is the point of allowing local function declarations?Code:void foo (void) { void bar(void); bar(); } void bar (void) { }
Well, in this example bar is not in the global scope when foo is defined, so you need a prototype somewhere.
This would be one way of "black-boxing" or hiding internal functions. If you have a source include with two functions called externally, foo1 and foo2, and foo1 and foo2 both use another function, bar, that is only used by them, then only foo1 and foo2 need know about bar.
I don't think it is very common tho, as it has some short-comings (bar will not be really hidden after the include).
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I did think of it for hiding internal functions, but simply hiding the prototype by making it only available to the functions that need it seemed like a half-way solution, and the name still has to be globally unique unless it's file local (in which case it needs to be at the bottom, else all functions after it can see it without a local prototype)
Therefore I was wondering if it had any other uses, but it seems not.
Now, if C also supported local defintions of functions, that would be more useful, but it seems not, as my compiler throws an error in such case ("Local function definitions are illegal").
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
That would needlessly complicate the language. I don't see why a declaration which is valid in one place should be invalid at another place.
To a certain degree, function-local prototypes promote encapsulation by hiding the declaration of some helper function everywhere except the one spot where it's used, but I rarely see them used for such a purpose -- usually, the programmer is simply too lazy to page up to the top of the file and include the appropriate header.
Once or twice, I've been in a situation where I needed to call some function in some library, but including the header file for that library was, for whatever reason, not appropriate, so I manually prototyped the function somewhere close to where I needed to use it. But having to do such a thing is awful, because it indicates the library is organized incorrectly (people need access to some part of its functionality but want to ignore other parts of it -- the library should be split into multiple pieces)
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}