Thread: The point of local function prototypes?

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    The point of local function prototypes?

    As in:

    Code:
    void foo (void)
    {
      void bar(void);
    
      bar();
    }
    
    void bar (void)
    {
    }
    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?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    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").

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Mole42 View Post
    Now, if C also supported local defintions of functions, that would be more useful,
    Yeah, and and anonymous versions of same. It's not really a big deal tho, it just means keeping your code organized and your namespace straight.
    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

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Mole42 View Post
    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?
    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);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM