CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2008
    Posts
    9

    Function Prototypes

    I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Function Prototypes

    Ummm....why wouldn't you use them?

    If you didn't have function prototypes, you'd have to define every single function above main(). That's fine if you've only got three or four, but it would completely break the purpose of .h files.

    The point of separating definition from declaration is so that code which uses the interface of a function doesn't need to be re-compiled if the definition of the function changes (although it will need to be re-linked). You can't do that without prototypes.

  3. #3
    Join Date
    Oct 2004
    Posts
    296

    Re: Function Prototypes

    No.

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Function Prototypes

    Just wondering why many beginners think that removing compiler errors is all about programming!

    Every language has set of rules (like English has), and you must follow the rules. Compiler and Linker need information about your program code, so you must provide them the way they need.

    A global variable referenced in multiple CPP files is an example where you need to use 'extern' to tell the compiler. The (only) non-extern declaration will tell about variable to linker. It's not all about removing errors.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Function Prototypes

    Quote Originally Posted by FrankyBones
    I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?
    That sounds like a pretty good reason to me.

  6. #6
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Function Prototypes

    Quote Originally Posted by 7stud
    No.
    Well, there are others too. One is for the sake of good coding standards, so that other programmers can understand the code better.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  7. #7
    Join Date
    Jul 2007
    Location
    london
    Posts
    247

    Re: Function Prototypes

    well function prototypes let you see what a file does just by looking at the header

    if what your saying though is that the compiler/linker should assum the function is somewhere else in the file or in another file then scope issues could arrise or you would have to redefine the function which isn't allowed, where re-prototpying a function is allowed

  8. #8
    Join Date
    Jul 2007
    Posts
    38

    Re: Function Prototypes

    In addition,
    If we have two fucntions that each of them call the other, it won't be able to compile the code without having function prototyping.

  9. #9
    Join Date
    May 2004
    Posts
    340

    Re: Function Prototypes

    several points are here:

    first : declaring function prototype can make another checking on the function typing mistake, usually we find that the function prototype and the function implementation name is not the same.if we declare function prototype, when implementing it, we can simply copy the function name,and make the implemention below, it can make sure our typing correctness.

    second: if not giving function prototype,u will have to make sure it is called after it is declared, or it will give one such error, some function is not defined.

    regards,
    jolley

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Function Prototypes

    You're missing the point. There is no need for function prototype declarations. The complier could quite easily plant all the necessary code to call a function without ever being given any information about that function. The linker will sort out the actual address to replace the placeholder inserted by the compiler.

    What the function prototype gives is information to the compiler so that it can check at compile time that you are calling the function with the correct number, types and order of arguments. Function prototypes are not about avoiding compilation errors - they are about generating more of them, so that you get fewer run-time errors because of mis-called functions.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  11. #11
    Join Date
    May 2004
    Posts
    340

    Re: Function Prototypes

    You're missing the point.
    maybe, these points that i just give are based on personal favors alike,of course declaring function prototype is not a must if u have your own policy, but it has some advantages that i have listed, right? imo, i like declaring function prototype, just to make the code decent and more readable.
    Function prototypes are not about avoiding compilation errors
    i mean it can make sure the typing correctness,and if we make the function prototype and the function implementation name different, don't we receive a compilation error?
    regards,
    jolley.

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Function Prototypes

    Quote Originally Posted by jolley
    maybe, these points that i just give are based on personal favors alike,of course declaring function prototype is not a must if u have your own policy, but it has some advantages that i have listed, right? imo, i like declaring function prototype, just to make the code decent and more readable.
    How, exactly, does it make code decent (and I don't understand what you mean by "decent") and more readable?

    I was addressing the OP in my answer
    Quote Originally Posted by original post
    I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?
    I was stating the simple fact that function prototypes are not needed to avoid compliation errors. There are plenty of languages that work perfectly well without requiring you to declare a function prototype (or have the full function definition available) before you use them.

    Quote Originally Posted by jolley
    i mean it can make sure the typing correctness,and if we make the function prototype and the function implementation name different, don't we receive a compilation error?
    regards,
    jolley.
    You've just made my point exactly: without a function prototype, then the incorrect function call would not generate a compilation error (*). With the prototype, it does. Hence, prototypes are about generating compilation errors (for incorrect usage), not about avoiding them.

    Don't get me wrong - I'm in favour of function prototypes, but the reason is that they catch incorrect usage errors early rather than late; and they do that by generating compilation errors, rather than linker or run-time errors.

    (*) Yes, I know a C++ compiler will generate an error, but that's a semantic requirement of the language. There is no reason in principle, though, that any compiler has to issue an error if there is no prototype.
    Last edited by Graham; March 23rd, 2008 at 05:48 AM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #13
    Join Date
    Mar 2008
    Posts
    30

    Smile Re: Function Prototypes

    I made a test.

    Note: The numbers and colons at the left of the source codes are not included(line numbers). It should be compiled without the numbers and colons.

    Test1: NO PROTOTYPE, FUNCTION ABOVE main()
    1: #include <iostream.h>
    2: #include <stdlib.h>
    3:
    4: int Add(int FirstNumber, int SecondNumber) //the function
    5: {
    6: return (FirstNumber + SecondNumber);
    7: }
    8:
    9: int main() //main()
    10: {
    11: int firstNum = 5, secondNum = 10, sum;
    12: sum = Add(firstNum, secondNum);
    13:
    14: std::cout << "5 + 10 = " << sum << std::endl;
    15:
    16: system("pause");
    17: return 0;
    18: }

    Test1 Result
    -No error

    Test2: NO PROTOTYPE, FUNCTION BELOW main()
    1: #include <iostream.h>
    2: #include <stdlib.h>
    3:
    4: int main() //main()
    5: {
    6: int firstNum = 5, secondNum = 10, sum;
    7: sum = Add(firstNum, secondNum);
    8:
    9: std::cout << "5 + 10 = " << sum << std::endl;
    10:
    11: system("pause");
    12: return 0;
    13: }
    14:
    15: int Add(int FirstNumber, int SecondNumber) //the function
    16: {
    17: return (FirstNumber + SecondNumber);
    18: }

    Test2 Result
    -Line 7: Call to undefined function

    Test3: FUNCTION WITH PROTOTYPE BELOW main()
    1: #include <iostream.h>
    2: #include <stdlib.h>
    3:
    4: int Add(int FirstNumber, int SecondNumber); //the prototype
    5:
    6: int main() //main()
    7: {
    8: int firstNum = 5, secondNum = 10, sum;
    9: sum = Add(firstNum, secondNum);
    10:
    11: std::cout << "5 + 10 = " << sum << std::endl;
    12:
    13: system("pause");
    14: return 0;
    15: }
    16:
    17: int Add(int FirstNumber, int SecondNumber) //the function
    18: {
    19: return (FirstNumber + SecondNumber);
    20: }

    Test3 Result
    -No error

    Test4: FUNCTION WITH PROTOTYPE ABOVE main()
    1: #include <iostream.h>
    2: #include <stdlib.h>
    3:
    4: int Add(int FirstNumber, int SecondNumber); //the prototype
    5:
    6: int Add(int FirstNumber, int SecondNumber) //the function
    7: {
    8: return (FirstNumber + SecondNumber);
    9: }
    10:
    11: int main() //main()
    12: {
    13: int firstNum = 5, secondNum = 10, sum;
    14: sum = Add(firstNum, secondNum);
    15:
    16: std::cout << "5 + 10 = " << sum << std::endl;
    17:
    18: system("pause");
    19: return 0;
    20: }

    Test4 Result
    -No error
    Last edited by richard_tominez; March 25th, 2008 at 10:10 PM.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function Prototypes

    Quote Originally Posted by richard_tominez
    I made a test.

    Note: The numbers and colons at the left of the source codes are not included(line numbers). It should be compiled without the numbers and colons.
    There is no <iostream.h> on my compiler. The correct header is <iostream>, not <iostream.h>.

    Also, CodeGuru postings contains things such as code tags. You should use them when posting code.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function Prototypes

    Quote Originally Posted by FrankyBones
    I know that function prototypes are useful for avoiding things like compilation errors, but are there any other good reasons to use function prototypes?
    If you're talking about C++, then yes, the compilation errors will occur if you call a function that hasn't been declared. That declaration can come in the form of just a prototype, or the entire function body occuring before the call.

    Now, if you go back to the genesis of why function prototypes came about, it came from the "second edition" of the C standard. The first cut of the C language didn't have function prototypes, so this code would compile, run, and produce unpredictable results.
    Code:
    int main()
    {
        char x = 'c';
        int value = sqrt(x, 34);
    }
    This code is compilable by C. However, when you run it, expect weird things to happen, since the real sqrt() function takes a single double argument, and returns a double. Bugs like this existed like the plague in many 'C' programs back in the old days. So along come the language writers, and in the second cut of 'C', they came up with the prototype, so as to lessen these types of bugs.

    Now the above code when compiled with a modern 'C' compiler will still not produce an error, since prototypes are still not mandatory in 'C'. However if a prototype is introduced, then the code will produce a compiler error.
    Code:
    #include <math.h>
    
    int main()
    {
        char x = 'c';
        int value = sqrt(x, 34);  // now the C compiler will emit an error
    }
    Also on a side note --

    notice how many times we emphasize that when posting code, to include all headers and prototypes that the code is referring to (this is especially the case for 'C' code). The reason is that if the code is 'C', not including a header is perfectly valid, but the results of running the code can be radically different.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured