Thread: prototyping functions

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    prototyping functions

    Ok. Normally, I prototype functions like this:

    Code:
    //prototype
    int function(int var1, char var2, char *point, type variable);
    
    //function
    int function(int var1, char var2, char *point, type variable)
    {
    	code;
    	etc. etc. etc.;
    	[...]
    }
    But, I've also seen in some programs this:

    Code:
    //prototype
    int function();
    
    //function
    int function(var1,var2,point,variable)
    int var1;
    char var2;
    char *point;
    type variable;
    {
    	code;
    	etc. etc. etc.;
    	[...]
    }
    Apparently, both of them work. But my question is: What is the difference?

    Thanks!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The top one is the "new" ANSI way; the second one is the outdated K&R way.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Cool. Ok so, it is the exact same thing? Nothing special between both?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, if I read the standard correctly, the second form doesn't check for type consistency -- so you can't get warnings of the sort "passing double as argument 2 of function function, expecting char".

    It should also be mentioned that in this case "new" means "1989" (if not farther back).

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot. I'll just keep using the "new" way.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > It should also be mentioned that in this case "new" means "1989" (if not farther back).

    K&R2 uses the ANSI-style declarations, and that came out in 1988.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Prototypes were introduced in some compilers and textbooks before the 1989 ANSI Standard (or 1990 ISO standard) were ratified. New standards do not appear by magic: there is a committee process by which they are developed and quite a few compiler vendors and textbook authors attend committees or receive documentation. Hence they are able to anticipate contents of the standards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM