### function and dynamic aray

hi eveybody.
i have a question.plz help me.
can i use dynamics aray for argumant in function,in another way i wanna have a function and i wanna the number of my argumant not be constan,how can i do that,its possible?
 and i wanna the number of my argumant not be constan

what do you mean ? pass as many arguments as you want upon function call ? something like :
`foo( 1, 2, 3, 4, 5, 6, 7, 23, 123, 0, 1, 3, 765, 4 );`
i mean i wanna have a function and sometimes i give it 2argumant and sometimes 3 or...
This involves using variadic functions but it seems that is dangerous. Here's a way to avoid it.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849`` ``````#include #include void func(std::vector& p1, std::vector& p2, std::vector& p3) { //here is used type void; obviously may use another C++ type int integer1 = 7 * p1[2]; //using the 3rd integer to p1.push_back(integer1); //pass it as the 4th in the p1[] double double1 = 3. * p2[0];//the same with the double p2.push_back(double1); //in the p2[] values std::string proverb = p3[1] + p3[2] + p3[3]; p3.push_back(proverb); //and with the strings //---------------your code------------------- } int main() { std::vector params1; //declaring a list with integers(if necessary) params1.push_back(1); params1.push_back(-12); //filling the list with some values params1.push_back(34); //how many are necessary std::vector params2;//declaring another type of variables params2.push_back(1.234); //(if necessary) params2.push_back(2.345); //filling it std::vector params3;// if necessary another list of variables params3.push_back("Hello world!");// of string type params3.push_back("All is "); params3.push_back("well when "); params3.push_back("ends well!.."); func(params1, params2, params3);//calling the function std::cout << params1[3] << '\t' << params2[2] << "\n\n"; std::cout << params3[0] << "\n\n"; std::cout << params3[4] <<'\n';//verifying that called function works //here you can change the values(and the number) of the variables in the lists //params1/params2/params3 for a new call of the function. return 0; }`````` ```238 3.702 Hello world! All is well when ends well!..```
Last edited on
Topic archived. No new replies allowed.