Infinite Parameters | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Infinite Parameters

Please guys i need to see the syntax for declaring functions with infinite parameters in this languages.. c++ c# java ruby I want functions like Out(1,6,8,5) that can accept any number of arguments.All help is appreciated

19th May 2018, 6:06 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
4 Answers
+ 26
well .... 😏😏😏 Ruby also support optional parameter... def Out(*p) p p puts p.count end Out(1,6,8,5) (*p) --> takes parameters as arrays... (**p) --> takes as hash...😎😎😎 func(a:1, b:2) Ruby Rocks...✌️😏😏😏 _____________________________________ In C++ u can pass an array as parameter.. lol 🤣🤣🤣 #include <iostream> void Out(int *a, int size); int main() { int a[] = {1,6,8,5}; Out(a, sizeof(a)/sizeof(*a)); } void Out(int *a, int size) { for (int i = 0; i < size; i++) std::cout <<a[i]<<" "; } _____________________________________ C# also support optional parameter but I don't know if it can take 1 or more at a time.... U can do this 👇 static void Main(string[] args) { Out(1,6,8,5); } public static void Out(params int[] list) { for (int i = 0; i < list.Length; i++) { Console.Write(list[i] + " "); } } ____________________________________ for Java as Dev suggested...
19th May 2018, 7:13 AM
🌛DT🌜
🌛DT🌜 - avatar
+ 8
Jan Markus one day the other languages will attack you for trying to brainwash everybody into python!😂 You keep telling everybody to do python
19th May 2018, 6:22 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
+ 6
You might look to use Varargs in Java, but that's just an hidden array implementation. Take a look at this example code: private static void varargs (int ... args) { for (int i: args) System.out.print(i + " "); } This can have any number of arguments passed to it! varargs (3, 9, 4, 80); https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
19th May 2018, 6:14 AM
Dev
Dev - avatar
+ 4
Jan Markus i already knew how to that with py😂.the languages i listed dont seem to have an obvious way of doing it
19th May 2018, 6:13 AM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar