Yagiz Degirmenci
Yagiz Degirmenci

Reputation: 20648

Nim-Lang : How to accept arbitrary number of arguments in a procedure

Is there a way to accept arbitrary number of arguments with procedure in Nim?

Example if i want to get sum of multiple arguments, it would look something like this

proc sum_all(x,y,z: int): int {.discardable.} = 
    return x+y+z

In Python it would look something like this

def sum_all(*args):
    return sum(args)

Upvotes: 2

Views: 343

Answers (1)

Iambudi
Iambudi

Reputation: 56

Nim has varargs to accept arbitrary number of arguments

proc sum_all(numbers: varargs[int]): int {.discardable.} = 
    for number in items(numbers):
      result += number

echo(sum_all(1,2,3));

Upvotes: 4

Related Questions