Function prototypes - needed yes or no?

This is a discussion on Function prototypes - needed yes or no? within the C Programming forums, part of the General Programming Boards category; Hello, I am programming in C. I prefer to use function prototypes for my static functions in my *.c files. ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Function prototypes - needed yes or no?

    Hello,

    I am programming in C.

    I prefer to use function prototypes for my static functions in my *.c files. And I always declare them at the top of my code.

    However, another worker who has a lot of experience in C/C++. Seems to think that they are a waste of time. However without them you have to order you functions correctly as you could call one that is not defined further down the file.

    I think function prototypes are clearer as the person reading your code can see at the start of that file all the static functions that you are using. All other ones will be in the header file. Also you have the flexibility to present the functions in the order that you want to define them in.

    Many thanks your input,

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by steve1_rm View Post
    Hello,

    I am programming in C.

    I prefer to use function prototypes for my static functions in my *.c files. And I always declare them at the top of my code.

    However, another worker who has a lot of experience in C/C++. Seems to think that they are a waste of time. However without them you have to order you functions correctly as you could call one that is not defined further down the file.

    I think function prototypes are clearer as the person reading your code can see at the start of that file all the static functions that you are using. All other ones will be in the header file. Also you have the flexibility to present the functions in the order that you want to define them in.

    Many thanks your input,
    in the code we sell the following order is usedin the source files:

    headerfile includes

    type definitions

    module variable declarations

    static function prototypes

    module function bodies

    static function bodies


    This order is required by the coding style applied to all source code that customers could see in our firm
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Are they needed? YES! Absolutely.
    Without them, you will likely get warnings and even subtle bugs in your code, because the compiler will only guess what the function wants.
    It opens up a world of bugs, seeing as the compiler does not know what the function returns or what arguments it accepts.
    It can also lead to weird errors such as when returning pointers from a function, and running on an x64 system, you will likely get runtime bugs. Severe bugs.
    So YES, they are absolutely required.
    C99 requires them, and so does C++. In C90, they are deprecated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by steve1_rm View Post
    However, another worker who has a lot of experience in C/C++. Seems to think that they are a waste of time.
    There are many good reasons for using function prototypes, some of which have already been mentioned. To me, the most important one is being able to move function definitions around without having to worry about whether a static function's definition appears before its first use.

    Furthermore, there are examples where you'll get stuck without function prototypes. Imagine a function foo() which calls bar(), and at the same time bar() calls foo():

    Code:
    double foo()
    {
            /* foo */
           x = bar();
           /* more foo */
    }
    
    char *bar()
    {
            /* bar */
           x = foo();
           /* more bar */
    }
    If you've been exposed to functional programming for a while, this kind of mechanism will seem almost natural.

    All in all, function prototypes make things much easier almost for free. Besides, C99 requires them.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, there are two issues being discussed.

    1. Are prototypes for functions used within the current source-file necessary?
    No, not strictly speaking, as long as the function is defined before the call to the function. This is of course not always possible. But it's perfectly valid C or C++ to do so. Whether it follows one or another companies coding standards or not is a different issue.

    Coding standards and style have only vague connections with what is correct in the standard language - coding standards are there to help software engineers produce code that is the same, consistent, style across a large set of staff/source files. It helps someone who is familiar with one part of the system quickly be able to understand other parts, because things are done in a particular way, and according to a particular rule. It does not mean that code written to OTHER coding standards are, as such, less good, or indeed less correct. It just means that it follows a different coding standard. It may, if it's well written, be equally correct and equally good.

    I come from a Pascal background, and in Pascal, it is common practice to define all functions before calling them. If you do NEED to declare a function before it's definition (e.g. in mutual recursion) you have to add the extra keyword "forward" at the end of the function declaration, and then define the function later. But that is considered an exceptional way of doing things.

    The company I work for has some pretty strict rules about coding standards, but says very little about the exact order of functions in general. Of course, free functions aren't that common anyways, since we write most of the code in C++.

    2. Do you need to declare a prototype for functions that are not defined before first call: Yes, absolutely. As discussed, not doing so can lead to undefined behaviour in all versions of C and C++. In the latest versions (C99 and C++) it is an error to call a function that hasn't been at leat declared.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Replies: 5
    Last Post: 02-08-2003, 06:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21