Function Prototypes

This is a discussion on Function Prototypes within the C Programming forums, part of the General Programming Boards category; What are they used for and why are they necessary?...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    46

    Function Prototypes

    What are they used for and why are they necessary?

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    They are used so that the compiler 'knows what is coming', in a manner of speaking. When you compile something the compiler goes through your code line by line and if you have a reference to a function which it isn't familiar with then you will have problems.

    E.g. This will cause problems without a prototype.
    Code:
    /* Includes */
    
    int main()
    {
          RandomFunction();
          return 0;
    }
    
    int RandomFunction()
    {
         /* Do something */
         return 1;
    }
    But this won't have any problems without a prototype.
    Code:
    /* Includes */
    
    int RandomFunction()
    {
           /* Do Something */
          return 1;
    }
    
    int main()
    {
           RandomFunction();
           return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    46
    Thank you sir. So basically if you use prototypes then it won't matter what order you list your functions? Any other good reasons they are used?

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    I found them useful during a group assignment to help divide up the tasks, a function prototype along with a description of what the function should do is typically all that is needed
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Sometimes they are necessary. E.g. if you have functions A and B, and function A calls function B and function B calls function A, then you are going to have problems without them.

    e.g.
    Code:
    int FunctionA(int val)
    {
          /* Do something with val - example only */
         int val2 = val -5;
    
         FunctionB(val2);
    }
    
    int FunctionB(int val)
    {
         int val2 = val + 3;
         FunctionA(val2);
    }
    Bit of a useless piece of code, but it shows the principle. Without function prototypes, how do you get the code to execute? This can become quite important when you get onto larger projects and you have classes (C++, not C) which have to interact via pointers to each other. I've needed to use prototypes to provide forward declaration of classes which are only properly defined later in the source code.

    Edit:
    I found them useful during a group assignment to help divide up the tasks, a function prototype along with a description of what the function should do is typically all that is needed
    That is very true. Helps document exactly what is going on. Very useful for bigger projects.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    For larger projects where there are multiple c source modules, function prototypes are placed in .h files and accessed using the #include directive in each .c module. That way, each module is assured they are using the same consistent definitions.

    Prototypes are similar in concept to the purpose of "extern" variable declarations. They specify the data type so that the compiler can go ahead without getting stuck on missing definitions.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Hinted at by nonoob, the purpose of a function prototype is so the compiler can do a proper job of type checking arguments and return values being passed around and then whine at you when you attempt to pass a float* where a char is expected.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Question about function prototypes
    By Mikecore in forum C Programming
    Replies: 2
    Last Post: 11-20-2005, 04:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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