Is it possible to define a default argument value based on the function's internal variables at the moment of return?

function f(x, z = nothing)
   y = 2x
   tmp = z === nothing ? y^2 : z
   return x + y + tmp
end

Because y only exists in the function body, it can’t be used in the default arguments. Default arguments can depend on other arguments though:

julia> f(a, b=2a) = b
f (generic function with 2 methods)

julia> f(1)
2
6 Likes