How to conveniently pass arguments from a function to another?

function f(x=1, y=2, z=3; a=1, b=2, c=3)
    # How do I know if a/b/c is assigned a value?
    2 * g(x, y, z; a=a, b=b, c=c) # Do I have to pass the arguments one by one to g?
end

function g(x=1, y=2, z=3; a=1, b=2, c=3)
    [a, b, c]' * [x, y, z]
end

Q1: How do I know if some keyword argument is assigned a value in the definition of a function?

Q2: Do I have to pass the arguments one by one to another function? I mean is there any convenient method to extracting all the input arguments when defining a function?

function f(args...; kwargs...)
    # if you refer to a kwarg explicitly, they must br assigned or error
    2 * g(args...; kwargs...)
 # You can slurp amd splat them
end

function g(x=1, y=2, z=3; a=1, b=2, c=3)
    [a, b, c]' * [x, y, z]
end
julia> f(1,2,3; a=10)
46

A1. If you assign a keyword argument a default value such as nothing you can check for nothing to see if it has the default value or another value.

A2. You can slurp and splat the arguments as above.

1 Like

This means I can’t explicitly show the input arguments of the outer level function f in its definition?

Can I add more keywords to f then?

function f(args...; d=1, kwargs..., e=2)
    # if you refer to a kwarg explicitly, they must br assigned or error
    2 * g(args...; kwargs...) * d * e
end

Yes, but they have to come first.

function f(args...; d=1, e=2, kwargs...)
    # if you refer to a kwarg explicitly, they must br assigned or error
     2 * g(args...; kwargs...) * d * e
end
2 Likes