Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function taking a variable number of arguments

Tags:

In this document: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-XID_1

It mentions that when creating for loops we can use the shorthand of 0..3 and 0...3 to replace i = 0; i < 3; ++i and i = 0; i <= 3; ++i respectively.

All very nice.

Further down the document in the Functions and Closures section it says that functions can have a variable number of arguments passed via an array.

However, in the code example we see the ... again.

func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
} 

Is this a mistake? It seems to me that a more intuitive syntax would be numbers: Int[].

A few examples down we see another code sample which has exactly that:

func hasAnyMatches(list: Int[], condition: Int -> Bool) -> Bool {
like image 956
Richard Washington Avatar asked Jun 04 '14 11:06

Richard Washington


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 make a function take any number of arguments?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.

How do you accept 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.

How do you make a function accept any number of arguments in Python?

To make a function that accepts any number of arguments, you can use the * operator and then some variable name when defining your function's arguments. This lets Python know that when that function is called with any position arguments, they should all be captured into a tuple (which that variable will point to).


2 Answers

In case of all arguments are Int numbers: Int[] would be intuitive. But if you have code like this:

func foo(args:AnyObject...) {
    for arg: AnyObject in args {
        println(arg)
    }
}

foo(5, "bar", NSView())

output:

5
bar
<NSView: 0x7fc5c1f0b450>
like image 191
Bogdan Avatar answered Sep 25 '22 10:09

Bogdan


The type of parameter in sumOf are known as 'variadic' parameter. The parameters passed are accepted just as a group of elements and is then converted into array before using it within that function.

A great example would be this post.

Passing lists from one function to another in Swift

like image 29
Gokul Avatar answered Sep 22 '22 10:09

Gokul