function prototype

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • agsupriya86
    New Member
    • Mar 2007
    • 34

    function prototype

    In C/C++ do we really need to mention a function prototype everytime..

    if the function is defined before its being called..do we need its prototype?
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    #2
    Originally posted by agsupriya86
    In C/C++ do we really need to mention a function prototype everytime..

    if the function is defined before its being called..do we need its prototype?
    No..You don't need to have a prototype

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by Silent1Mezzo
      No..You don't need to have a prototype
      This is not correct.

      All functions in C++ (except main() ) require a function prototype.

      This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

      Unless you do this, your code will not compile.

      This is not true in C.

      Comment

      • Silent1Mezzo
        New Member
        • Feb 2007
        • 208

        #4
        Originally posted by weaknessforcats
        This is not correct.

        All functions in C++ (except main() ) require a function prototype.

        This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

        Unless you do this, your code will not compile.

        This is not true in C.
        Sorry I was merely answering for C since I'm not a C++ programmer. I guess I should have specified that.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by weaknessforcats
          This is not correct.

          All functions in C++ (except main() ) require a function prototype.

          This is either a) the entire function is defined before the first call or b) the function is defined in another file, and the first line of the function followed by a semi-colon appears before the first call.

          Unless you do this, your code will not compile.

          This is not true in C.
          You have missed the case (that is quite similar to b) of the function is being call from a place in the file that is before it's definition in which case it requires a declaration(pro totype) before it is called.


          Also Note, today although it is not a strict requirement in C to call a function in the presence of a prototype it would be considered bad practice to do this and many C compilers will output a warning or message (but not an error) of some sort if you do this.

          Comment

          • Girish Kanakagiri
            New Member
            • May 2007
            • 93

            #6
            Originally posted by agsupriya86
            In C/C++ do we really need to mention a function prototype everytime..

            if the function is defined before its being called..do we need its prototype?
            In C++ you have to put the prototypes in .h file and include the same
            in .cpp file in which you should give the implementation.
            Finally you can have the main in any other file, in which you will be calling
            this function. As a good practice always put the function prototype.

            Regards,
            Girish

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by Girish Kanakagiri
              In C++ you have to put the prototypes in .h file and include the same
              in .cpp file in which you should give the implementation.
              This is not true, the prototype can go anywhere including in a cpp file.

              If you wanted to you could put the function in 1 cpp file and then separately put a prototype in every other cpp file you wish to call it from. However this would be extremely bad practice and asking for errors later in project if the function prototype got change.

              The only stipulation is that a prototype has been seen before it is called. Good practice states that you should only have 1 prototype for any given function in your code, in a header file if that function is to be called from multiple other files or in the cpp file itself if it is a static function.

              This is the difference between what you can do and what it is a good idea to do in order to produce maintainable code that is more likely to be bug free.

              C++, like C before it, allows you to do all sorts of things that would be considered bad practice. It is up to the programmer to avoid doing them in order to increase the quality of their own code.

              This is what a coding standard is about.

              Comment

              Working...