Functions in function args?

Hi all!

I am wondering – in R, you can default function argument defaults with functions, e.g.

tmpfun ← function(a, b=2*a)
{
return(a+b)
}

tmpfun(a=1)

Returns: 3

…is anything like this possible in Julia? It’s not crucial, but it’s handy.

Cheers!
Nick

You mean something like this?

julia> f(;a, b=2a) = a + b
f (generic function with 1 method)

julia> f(a=1)
3
4 Likes

Hi! Thanks, I meant e.g. using a function to define a default value for a variable. It looks like this actually works, I must have messed something else up when I was having problems yesterday.

This works:

function f1(a)
return 2*a
end

function f2(b, c=f1(b))
return b*c
end

f2(2)

Returns: 8

please format your code with ```

function f1(a)
    return 2*a
end

function f2(b, c=f1(b))
    return b*c
end

f2(2)
# Returns: 8
2 Likes