Thread: variable arguments in functions

  1. #1
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118

    variable arguments in functions

    Hi,

    I can define a function like this

    Code:
    int someFunction(int x, int y)
    {
        some code
    }
    But I would like to have a variable argument. For example I can make the above function find the mean average of two numbers. But I would like to have it find the mean average of a variable number of numbers, namely the numbers defined in a vector in another part of the program. i.e. The user enters how many numbers he would like to find the average of, then enters those numbers, and the function finds the average. I tried something like this

    Code:
    int someFunction(somePreviouslyDefinedVector)
    {
        some code
    }
    But this was pretty much a stab in the dark and I wasn't surprised when it didn't compile :P

    Is it a pretty complicated technique, or is it a simple fix?

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2008
    Location
    Philadelphia, PA
    Posts
    2
    You could take the args in a STL vector. That would be the simplest solution. You can then use the member function size().

    Rahul

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But I would like to have it find the mean average of a variable number of numbers, namely the numbers defined in a vector in another part of the program. i.e. The user enters how many numbers he would like to find the average of, then enters those numbers, and the function finds the average.
    Instead of using variable arguments, I suggest using the idiom of a range denoted by an iterator pair. One iterator points to the first element of the range, the other element points to one past the last element of the range. If the callers wants the mean of the entire vector, the caller would pass the iterators returned by vec.begin() and vec.end() for a vector named vec.

    This idiom is used by the standard library containers and generic algorithms, and in fact you can already calculate the mean by using std::accumulate to calculate the sum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since the numbers are located in a vector, why not pass the vector itself, as rahulss suggests?
    Other solutions are more complicated. More than they need to be, for this.
    Last edited by Elysia; 02-21-2008 at 11:49 AM.
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since the numbers are located in a vector, why not pass the vector itself, as rahulss suggests.
    Other solutions are more complicated. More than they need to be, for this.
    Passing a range would allow one the flexibility of switching to another container, and in the end one may end up using iterators within the function anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use the std::accumulate() function in <numeric> to add all numbers in the range (eg. v.begin() to v.end() ), then divide by the number of elements in the range.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    And if, by any chance the arguments don't share a similar type you can use boost::any with the suggestion produced by laserlight by simply passing beginning and end iterators to it.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And what would you do with the any, then?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118
    I will admit that I don't fully understand most of the answers given here. But I'm still learning so maybe I was going a little over my head. Whenever I learn a new technique I always try to create an idea for a program using those techniques and sometimes I ask a bit much of myself!

    Thanks to all for the kind replies

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by CornedBee View Post
    And what would you do with the any, then?
    Don't follow, CornedBee
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Assuming you pass a vector of boost::anys. What would you do with them inside the function?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. I see your point. Makes no sense, does it?
    I would still have to convert them or find their types. Thank you for the correction.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  2. How slow are variable arguments?
    By dwks in forum C Programming
    Replies: 5
    Last Post: 09-18-2005, 12:11 PM
  3. Variable number of arguments
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2004, 08:58 AM
  4. passing arguments to functions
    By Micko in forum C Programming
    Replies: 4
    Last Post: 07-18-2004, 10:59 AM
  5. Functions with a variable number of arguments
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 01:12 AM