variable arguments in functions

This is a discussion on variable arguments in functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I can define a function like this Code: int someFunction(int x, int y) { some code } But I ...

  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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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 10: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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Portugal
    Posts
    7,467
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,893
    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
    Portugal
    Posts
    7,467
    Quote Originally Posted by CornedBee View Post
    And what would you do with the any, then?
    Don't follow, CornedBee
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,893
    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
    Portugal
    Posts
    7,467
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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

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