+ 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
4 Respostas
+ 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...
+ 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
+ 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
+ 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