function declarations, global/within a function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Douglas

    #1

    function declarations, global/within a function?

    Hi,

    When and why would you want to...

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

    int someFunction(vo id);//declaration

    someFunction();

    }

    int someFunction(vo id){} //definition

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


    instead of.....

    2)
    //=============== ===============
    int someFunction(vo id); //declaration


    int main(){

    someFunction(vo id);

    }
    int someFunction(vo id){} //prototype

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


    Thanks, Douglas
  • Richard Bos

    #2
    Re: function declarations, global/within a function?

    mm2ps@yahoo.co. uk (Douglas) wrote:
    [color=blue]
    > When and why would you want to...[/color]
    [color=blue]
    > int main(){
    > int someFunction(vo id);//declaration
    > someFunction();
    > }
    > int someFunction(vo id){} //definition[/color]
    [color=blue]
    > instead of.....[/color]
    [color=blue]
    > int someFunction(vo id); //declaration
    >
    > int main(){
    > someFunction(vo id);
    > }
    > int someFunction(vo id){} //prototype[/color]

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

    Richard

    Comment

    • Herbert Rosenau

      #3
      Re: function declarations, global/within a function?

      On Mon, 5 Jul 2004 11:54:49 UTC, mm2ps@yahoo.co. uk (Douglas) wrote:
      [color=blue]
      > Hi,
      >
      > When and why would you want to...
      >
      > 1)
      > //=============== ==============
      > int main(){
      >
      > int someFunction(vo id);//declaration[/color]

      This makes the prototype of someFunction only visible in main but not
      aywhere in the translation unit before the definition of it occures.
      [color=blue]
      > someFunction();
      >
      > }
      >
      > int someFunction(vo id){} //definition
      >
      > //=============== =============== =
      >
      >
      > instead of.....
      >
      > 2)
      > //=============== ===============
      > int someFunction(vo id); //declaration[/color]

      This makes the prototype of the function visible throuth the whole
      translation unit beginning at this point.
      [color=blue]
      >
      > int main(){
      >
      > someFunction(vo id);
      >
      > }
      > int someFunction(vo id){} //prototype
      >
      > //=============== =============== ==
      >
      >
      > Thanks, Douglas[/color]

      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

      Comment

      Working...