Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is function prototyping dead?

Is function prototyping dead?

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    A semi-popular IDE compiles my C++ code without declaring function prototype. Did I missed the newsflash or just an compiler option? Seems odd.

    L J 2 Replies Last reply
    0
    • V Vaclav_

      A semi-popular IDE compiles my C++ code without declaring function prototype. Did I missed the newsflash or just an compiler option? Seems odd.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You only need prototypes for forward declarations. And IDEs do not do the compiling, that's the compiler's job.

      V 1 Reply Last reply
      0
      • V Vaclav_

        A semi-popular IDE compiles my C++ code without declaring function prototype. Did I missed the newsflash or just an compiler option? Seems odd.

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        A function definition is also a declaration if there was none so far. The compiler (not the IDE) will add it to it's internal declaration list when processing such a function.

        1 Reply Last reply
        0
        • L Lost User

          You only need prototypes for forward declarations. And IDEs do not do the compiling, that's the compiler's job.

          V Offline
          V Offline
          Vaclav_
          wrote on last edited by
          #4

          True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?

          L J 2 Replies Last reply
          0
          • V Vaclav_

            True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            When the compiler comes across a function call it checks its tables for a declared prototype. If it cannot find one then it will infer a definition from the parameters of the function call. When it then finds the actual function it will check that the definition matches the stored declaration. If it does, then all well and good. If it does not then it will throw out an error message. As to your last question, a prototype is a forward declaration. It just tells the compiler the type of the parameters and return value of the function, so it can check on any calls made to the function before it is found in the source. The same applies to external functions whose prototypes are in .h files.

            1 Reply Last reply
            0
            • V Vaclav_

              True, but I have been using IDE whose authors are very fond of their "beginners feature " which actually builds prototypes "for you". So they claim. I am now test driving another IDE, still based on GCC and the first test function I used compiled without prototype, that is why I made this post. Next function failed without prototype, so I am not sure what is going on. So what is the difference between prototype and forward declaration? Or is there a difference?

              J Offline
              J Offline
              Jochen Arndt
              wrote on last edited by
              #6

              A declaration (prototype) describes an object (function, class, variable).

              int some_function(int some_arg);

              A definition (implementation) implements an object.

              int some_function(int some_arg)
              {
              int result = 0;
              // some code
              return result;
              }

              If there is no declaration for an object, the definition implies the declaration. The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. While strictly speaking all declarations are forward declarations, the term is mainly used with function declarations in header files where a pointer or reference to another class is passed as function argument. An example:

              // Header file for MyClass

              // Forward declaration for OtherClass.
              class OtherClass;

              class MyClass
              {
              MyClass();
              SomeFunction(OtherClass &otherClass);
              };

              The implementation file of MyClass must still include the header file containing the OtherClass declaration. But the above header file must not do so which reduces the compile time. More important, this must be used when both classes use the other which would normally require that both header files include each other which would lock the compiler (recursive inclusion). See also this SO thread: c++ - What is the difference between a definition and a declaration? - Stack Overflow[^]

              V 1 Reply Last reply
              0
              • J Jochen Arndt

                A declaration (prototype) describes an object (function, class, variable).

                int some_function(int some_arg);

                A definition (implementation) implements an object.

                int some_function(int some_arg)
                {
                int result = 0;
                // some code
                return result;
                }

                If there is no declaration for an object, the definition implies the declaration. The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. While strictly speaking all declarations are forward declarations, the term is mainly used with function declarations in header files where a pointer or reference to another class is passed as function argument. An example:

                // Header file for MyClass

                // Forward declaration for OtherClass.
                class OtherClass;

                class MyClass
                {
                MyClass();
                SomeFunction(OtherClass &otherClass);
                };

                The implementation file of MyClass must still include the header file containing the OtherClass declaration. But the above header file must not do so which reduces the compile time. More important, this must be used when both classes use the other which would normally require that both header files include each other which would lock the compiler (recursive inclusion). See also this SO thread: c++ - What is the difference between a definition and a declaration? - Stack Overflow[^]

                V Offline
                V Offline
                Vaclav_
                wrote on last edited by
                #7

                The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. Perfect answer - that is exactly why my test function - first one in header file - did not need declaration. Jochen, I really appreciate your help - always right to the point. Thanks PS Are you by any chance teacher / instructor ? You sure know how to explain stuff. Cheers Vaclav

                J 1 Reply Last reply
                0
                • V Vaclav_

                  The compiler needs a declaration when an object is used the first time (accessing a variable, calling a function). So there is no need for a declaration if the first usage is located after the definition in the source code. Perfect answer - that is exactly why my test function - first one in header file - did not need declaration. Jochen, I really appreciate your help - always right to the point. Thanks PS Are you by any chance teacher / instructor ? You sure know how to explain stuff. Cheers Vaclav

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #8

                  Thank you. I'm not a teacher. I'm an R&D engineer (hardware and software).

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups