Thread: function arguments

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    function arguments

    Hello

    I've recently noticed that some of you define functions (prototypes) this way:

    Code:
    void some_function(int, int, int);
    instead of

    Code:
    void some_function(int a, int b, int c);
    Which way is better?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It depends. In most cases I would say the second one would be better assuming you provided descriptive variable names. That's because it helps to document to the user of the function what the parameters are supposed to mean.

    The only case where it might be better to use the first method is for private implementation functions in a class. The implementation of those doesn't need to be known by the user of the class, so it may make sense to hide their meanings.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Which way is better?
    The only time the first option is better, is when you have unused parameters. This can suppress warnings about not using the parameter, and otherwise it's best to name your parameters so that the code documents itself.
    My best code is written with the delete key.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Prelude View Post
    >Which way is better?
    The only time the first option is better, is when you have unused parameters. This can suppress warnings about not using the parameter, and otherwise it's best to name your parameters so that the code documents itself.
    But don't count on it. At least once I have seen screwy code of the form:

    Code:
    int foo(int a, int b, int c);
    
    int foo(int b, int a, int c)
    {
       ...
    }
    Can you guess how long it took me before I realized what was happening? Yeah.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But don't count on it.
    I was assuming a conscientious programmer writing good code. Give me a set of style guidelines and I can follow them to the letter, but still produce an unintelligible mess. If you take into account bad programmers, it's impossible to guarantee readable code and we should all take up burger flipping.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. Arrays as function arguments :(
    By strokebow in forum C Programming
    Replies: 10
    Last Post: 11-18-2006, 03:26 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM