Thread: Functions taking ? # of args

  1. #1
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972

    Functions taking ? # of args

    What is the best way to create a function that takes an undeermined amount of arguments?

    I remember reading about using an elipsis (...) I think, and I've seen functions, for example, in SDL where it takes more than one using "|" ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) ) . How does that work anyway?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    you could try using a macro, but it would only be useful to you in rare circumstances, i.e.

    Code:
    #define Square(x) (x*x)
    You could pass Square(2 + 4); which would expand to 2 + 4 * 2 + 4. Or you could try to overload the function and have default paramters, i.e.

    void func(int a, int b, int c, int d = 3)
    void func(int a, int b, int c = 2, float d = 3.3)
    void func(int a, char b = 'c', int c = 2, float d = 3.3)

    Only ideas for you to play at.
    Be a leader and not a follower.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Look up va_list and va_arg in the <cstdarg> header. That'll allow you to write functions with many parameters (printf style).

    The example you gave is only one argument. The '|' operator (inclusive-or) just operates on those two arguments and passes the result to the function. Conceptually, it would be the same if '+' replaced '|'.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "I remember reading about using an elipsis (...) "

    You are misunderstanding what the ellipsis does. You cannot access any of the arguments you send to a function with ellipsis. So, essentially you are calling a function with no parameters--not multiple parameters. Based on that fact and what Zach L. told you, it's most likely you've never seen a function with an indeterminate number of parameters.


    You might also check out default parameter values for functions to see if that might work for you. That allows you to send say 4 parameters to a function with 10 parameters. With default values, you can have the rest of the parameters assigned 0 or 1 or whatever works for you.
    Last edited by 7stud; 06-04-2003 at 09:05 PM.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thanks for your suggestions guys. What I really was looking for was something like Zach said, since its not really a problem if I know it wont go over a certain amount of arguments. (I was thinking rather than taking up to, say 5 args, taking 5 or more args)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by 7stud
    "I remember reading about using an elipsis (...) "

    You are misunderstanding what the ellipsis does. You cannot access any of the arguments you send to a function with ellipsis. So, essentially you are calling a function with no parameters--not multiple parameters.
    Not quite so. You can access the unnamed arguments with va_list, va_arg, and va_end. printf is an example of a function with an undetermined number of arguments.

    I do caution you against using this though. One, it is difficult to predict how many arguments have been passed, and two, there is no type checking on these arguments. As you might imagine, this is a rather error-prone technique, mainly left in place to accomodate the existence of printf and its relatives.

    Chances are 7stud's idea of default parameters will provide a better solution to the problem you are working on.

    Also, a bit more on that SDL example you gave. Lets say you have 8 options for a function which can be in one of two states (on or off). Then, each of those options can be represented by its own bit in a char - one option uses the value of the least significant bit, and so on.

    For example, you have options a and b, and this is what they look like:

    a (on) 00000001
    a (off) 00000000

    b (on) 00000010
    b (off) 00000000

    Then a and b both on could be represented as 00000011. This enables you (in some cases) to be more space efficient with arguments.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    void func(int a, int b, int c, int d = 3)
    the way you said that his idea would work better made it sound like i could pass more than 4 arguments to that function. is that what you meant? if so, how?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    No you cannot pass more than 4 arguments to that function. And, for the bit method, you would pass one integer, but once again that would have a maximum number of bits that you have decided beforehand represent something.

    What are you trying to do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. taking Function's address
    By arjunajay in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2005, 05:55 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM