# Thread: Newbie question about Variable Argument Lists for Functions

1. ## Newbie question about Variable Argument Lists for Functions

If you have a function which can accept a variable number of arguments, how can you find out how many arguments were passed to it?

2. Either manually (passing number of arguments as an argument), or by fetching argument by argument until you come across a "known value" that is used to mark the end of arguments (ie NULL), or if the arguments are all of the same type, you can use a vector and use its .size() method.

3. Incidentally, are you sure that you need variable argument lists in the first place?

4. Awesome, thanks. And thanks for replying with the "safe delete" function in my other thread, too!

5. Originally Posted by laserlight
Incidentally, are you sure that you need variable argument lists in the first place?
Nope, but that's where I am in the tutorial, ahaha
Give me a little time... I'm still in the crawl stage of "crawl, walk, run"!

6. Originally Posted by Elysia
Either manually (passing number of arguments as an argument), or by fetching argument by argument until you come across a "known value" that is used to mark the end of arguments (ie NULL), or if the arguments are all of the same type, you can use a vector and use its .size() method.
it's probably worth mentioning that you cannot use differing types in va lists without some additional effort/sophistication either, so using a vector is generally a good choice.

7. Unless the function expects different types, in order. But generally, you are right, of course.

8. yes, that is true if there is an assumed repeating pattern to the types passed. however it may well still be preferable, even if arguably marginally less convenient, to pass a vector of structs.

just yesterday i made a polynomial function that uses a variable argument constructor, where the constructor arguments go: unsigned int order, int exp1,double coeff1,...int expn,double coeffn

and what did i end up having to do immediately thereafter?

declaring
Code:
`struct ecpair{int exp;double coeff;}`
within the body of polynomial to store the data in a vector. in then end it would have been vetter to just pass a vector<polynomial::ecpair>

the only thing i can really see variable argument functions providing unique functionality is is where the first argument is a typelist, as then there need be no pattern, but then my compiler doesn't match the function so it doesn't work anyway. :\

Popular pages Recent additions