1

I would like to create a function in Julia which accepts an optional argument let's call it "BMI", which is itself a function, such that, if this optional argument is not included, "do_something" skips a block of instructions.

That is, something like

function do_something(age, height; BMI=None)
         print("hi, I am $age years old and my height is $height")
         if window!=None
               print("My BMI is $(BMI(age,height))")
         end
         print("bye")
end

What is the best way to accomplish this in Julia?

math_lover
  • 886
  • 7
  • 20
  • This approach is fine. You may also want to create two functions with different number of arities (i.e. do_something(age,height) and do_something(age,height; BMI)) to get rid of the if block (and maybe can make your code clearer). – whilrun Jun 24 '20 at 01:28
  • @whilrun Yes I am precisely trying to avoid creating two functions, as the actual function I'm writing is much more complicated. Thus I want to implement something like the pseudocode above, but how would that be done in Julia? "None" doesn't exist in julia and not sure how to check if we are equal to empty value either. – math_lover Jun 24 '20 at 01:41
  • 1
    Julia's None is `Nothing`, and you can just use if BMI == Nothing just like any other language with None. – whilrun Jun 24 '20 at 01:55
  • It should be `nothing` instead of `Nothing`. – DNF Jun 24 '20 at 07:27

1 Answers1

2

There are few approaches to your problem. First of all, you may use nothing to distinguish whether a BMI was passed to your function

function do_something(age, height; BMI = nothing)
         print("hi, I am $age years old and my height is $height")
         if !isnothing(BMI)
               print("My BMI is $(BMI(age,height))")
         end
         print("bye")
end

If you are on an older version of Julia (I think 1.1 or lower) you should use BMI !== nothing, take notice of double equal sign. There are reasons why it is better than use !=. It may not look important in your particular case, but it is better to make good habits from the start.

But at the same time, I would recommend to use multiple dispatch, which may look excessive here, but it gives you taste and feel of Julia and also make it possible to naturally extend your initial declaration

do_bmi(bmi::Nothing, age, height) = nothing
do_bmi(bmi, age, height) = print("My BMI is $(bmi(age,height))")

function do_something(age, height; BMI = nothing)
         print("hi, I am $age years old and my height is $height")
         do_bmi(BMI, age, height)
         print("bye")
end

For example, if you would like to give user possibilty to choose BMI from the set of predefined functions, abbreviated by some String, all you have to do is define this function

function do_bmi(bmi::AbstractString, age, height)
  if bmi == "standard"
    do_bmi((a, h) -> a^2/h, age, height)
  else
    println("Unknown BMI keyword $bmi")
  end
end

and call your original function like this

do_something(20, 170, BMI = "standard")
Andrej Oskin
  • 2,284
  • 14
  • 11