function declarations, global/within a function?

Discussion in 'C Programming' started by Douglas, Jul 5, 2004.

  1. Douglas

    Douglas Guest

    Hi,

    When and why would you want to...

    1)
    //=============================
    int main(){

    int someFunction(void);//declaration

    someFunction();

    }

    int someFunction(void){} //definition

    //===============================


    instead of.....

    2)
    //==============================
    int someFunction(void); //declaration


    int main(){

    someFunction(void);

    }
    int someFunction(void){} //prototype

    //================================


    Thanks, Douglas
    Douglas, Jul 5, 2004
    #1
    1. Advertising

  2. Douglas

    Richard Bos Guest

    (Douglas) wrote:

    > When and why would you want to...


    > int main(){
    > int someFunction(void);//declaration
    > someFunction();
    > }
    > int someFunction(void){} //definition


    > instead of.....


    > int someFunction(void); //declaration
    >
    > int main(){
    > someFunction(void);
    > }
    > int someFunction(void){} //prototype


    You wouldn't, really. I can't think of a single reason why
    well-structured could should need that.

    Richard
    Richard Bos, Jul 5, 2004
    #2
    1. Advertising

  3. On Mon, 5 Jul 2004 11:54:49 UTC, (Douglas) wrote:

    > Hi,
    >
    > When and why would you want to...
    >
    > 1)
    > //=============================
    > int main(){
    >
    > int someFunction(void);//declaration


    This makes the prototype of someFunction only visible in main but not
    aywhere in the translation unit before the definition of it occures.

    > someFunction();
    >
    > }
    >
    > int someFunction(void){} //definition
    >
    > //===============================
    >
    >
    > instead of.....
    >
    > 2)
    > //==============================
    > int someFunction(void); //declaration


    This makes the prototype of the function visible throuth the whole
    translation unit beginning at this point.

    >
    > int main(){
    >
    > someFunction(void);
    >
    > }
    > int someFunction(void){} //prototype
    >
    > //================================
    >
    >
    > Thanks, Douglas


    This makes sense whenever you have a fuction you would use only in a
    specific point. Here you says in the first exapmle that only main()
    should know how to call somFunction() but no other fuction that is
    declared before the definition of someFunction() itself what is a
    declaration on itself.

    Shortening the visibility of something to the only point it is really
    needed in a translation unit can help to find mistakes in coding much
    earlier than in golden code. As any try outside the points where the
    prototype should be known gives you a diagnostic from the compier, so
    you would check if either your code or your design has a flaw long
    before you starts testing.



    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    Herbert Rosenau, Jul 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Theese

    Local function declarations

    Dave Theese, Sep 5, 2003, in forum: C++
    Replies:
    1
    Views:
    451
    Jack Klein
    Sep 5, 2003
  2. Elliot Marks

    global struct declarations

    Elliot Marks, Jan 7, 2004, in forum: C Programming
    Replies:
    20
    Views:
    855
    Thomas Stegen
    Jan 10, 2004
  3. Chris  Portka
    Replies:
    2
    Views:
    647
    Michael DOUBEZ
    Dec 5, 2006
  4. Alex Hall
    Replies:
    5
    Views:
    228
    Lawrence D'Oliveiro
    May 11, 2010
  5. vunet

    Set Global Variable Within a Function

    vunet, Feb 17, 2010, in forum: ASP General
    Replies:
    5
    Views:
    1,508
    Bob Barrows
    Feb 17, 2010
Loading...

Share This Page