5
votes

In R you can use NULL arguments in functions like myfun = function (a=NULL, b, c) can you do this in Julia? I'm asking because I'd like variable a as a condition : if a = NULL do this, else do that. I can just write different functions of course but they would mostly repeat each other. I can also just assign arbitrary numbers but it seems using NULL is more clear. Thanks!.

1
Perhaps you need Nullable{Int64}() if it is a integer64akrun
The equivalent of NULL is nothing, of type Nothing. If you can, it's better to avoid it in function arguments, but at times it's fine. It's a subtle issue.PatrickT

1 Answers

5
votes

One of the most common questions from people coming to a new language is how to code exactly like in the language they come from. But testing for NULL is not likely to be the best way to implement this in Julia. Mostly you'll define two methods for your function:

function myfun(a, b, c)
    ...
end

function myfun(b, c)
    ...
end

With the different behaviours.