How to reference a function's arguments inside the function as a list

I have a general boilerplate coding workflow..

function dosomething(df, a, b, c, d .. n)
push!(df, [ all those arguments ])
end

In other words, I would like to find a way where I don’t have to hand transcribe the input values for push into an array. There has to be some way to reference the parameter list given in the function, definition and simply push the declared argument list into an array, so I don’t have to manually type this out all the time when I’m writing a function.

Is there a clean way to do this?

If you specify that many arguments by name, those are the names you have to work with in the method body. There’s no automatic hidden argument name for a collection of a trailing subset of those arguments, and you didn’t specify that subset anyway.

What does work for a trailing subset of arguments are Varargs methods. If you don’t need those individual argument names, you can just make a name for the collection (a tuple):

function dosomething(df, args...) # method assigns tuple containing trailing inputs to args
    push!(df, args...) # call separates args... into separate inputs
end

dosomething(A, a, b, c, d)

n input values is a lot, which makes me suspect those input values were in a collection to begin with. If so, there’s no need to index every element before insertion to another collection, just use append!.

1 Like