Question about function prototypes

This is a discussion on Question about function prototypes within the C Programming forums, part of the General Programming Boards category; I am doing some home work for class and we are learing about pointers. anyhow I was keying in a ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Question about function prototypes

    I am doing some home work for class and we are learing about pointers.
    anyhow I was keying in a example program from the book. and I noticed
    that it defined a function prototype before main and then in that function
    it defined another function prototype. I compiled and ran the program fine.

    My Question is I thought you are supposed to define all your functions before main
    and not in another function. or am I wrong and the fact that they define it in a function
    just makes it a local function and not a global one?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    - A function must be either defined or prototyped, or both, before it is called.
    - You can define it fully before you call it.
    - You can prototype it before you call it.
    - You must do one or the other; or both.
    - You may only define a function once.
    - You may prototype a function as often as you like, provided all prototypes are the same with regards to arguments and the return type.

    All a prototype does is let the compiler know what arguments and what return type the function is to have, this way it can tell if you're being stupid or not by using it incorrectly.

    A local prototype limits the scope of the function, assuming it hasn't been defined already, or prototyped already.
    Code:
    void foo( int x )
    {
        void bar( void ); /* prototype this for use in 'foo' */
        ... do whatever ...
        bar( ); /* call bar */
    }
    
    int main( void )
    {
        bar( ); /*illegal, main does not know of 'bar'*/
    
        foo( 5 ); /* legal, main knows of 'foo' */
    }
    
    void bar( void )
    {
        ...whatever...
    }
    [edit] Added scope info. [/edit]

    Quzah.
    Last edited by quzah; 11-20-2005 at 04:42 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Thanks! yup I just read about scope in my book when I seen you added this to your post

    So the second prototype function is a local function that can only be called from within the function that it was defined or prototyped in.
    Last edited by Mikecore; 11-20-2005 at 04:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. function pointer question
    By andrea72 in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2007, 05:25 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21