PF111
PF111

Reputation: 41

Julia: Equivalent of *args from Python in Julia?

Is there an equivalent command to *args from Python in Julia? I ask because I am trying to write a numerical integration function, the argument of which is a function that may depend on several constants. For example, if my Julia function was

function func(x,a,b)
return a*x + b
end

is there way that I could make an array

args = [a,b]

and call this function as

val = func(x,*args)?

Upvotes: 4

Views: 1003

Answers (1)

AboAmmar
AboAmmar

Reputation: 5559

This is quite easy in Julia. Putting ... after the last argument in a function definition makes it accept any number of arguments passed as a tuple:

function func(x,args...)
    result = zero(x*args[1])
    for (i,arg) in enumerate(args)
        result += arg * x^(length(args) - i) 
    end
    result
end

and you can call it either way:

args = [a, b]
val = func(x, args...)

# or
val = func(x, a, b)

Upvotes: 6

Related Questions