Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with variable number of arguments

Tags:

java

function

c#

As the title says I need to know if there is a corresponding syntax as java's ... in method parameters, like

void printReport(String header, int... numbers) { //numbers represents varargs   System.out.println(header);   for (int num : numbers) {      System.out.println(num);   } } 

(code courtesy of wikipedia)

like image 712
Gabber Avatar asked Mar 20 '12 10:03

Gabber


People also ask

Which function accepts a variable number of arguments?

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments.

How do you write a function with variable number of arguments in Python?

The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.

What is variable argument function?

A variable argument function (variadic function) is a function that can accept an undefined number of arguments. In many programming languages, formatted output functions are defined as variadic functions. In C++, variable argument functions are declared with the ellipsis (...) in the argument list field.


1 Answers

Yes you can write something like this:

void PrintReport(string header, params int[] numbers) {     Console.WriteLine(header);     foreach (int number in numbers)         Console.WriteLine(number); } 
like image 140
Adriano Repetti Avatar answered Oct 05 '22 12:10

Adriano Repetti