Functions with optional positional arguments in Julia (explanation with examples)

Written by Administrator on Monday May 11, 2020

Sometimes we need a function, which requires several arguments. It's normal situation and this is the way functions work normally almost in all programming languages. Optional arguments will make your code easier and more powerful with decreasing a number of functions you create.

Example No.1 : Calculating engine consumption

For example, let's create a function, which returns fuel consumption depending on engine volume (cubic centimeters = cc).

function ShowConsumption(cc)
    return round(cc/170)          # rounding float-number result to the 2nd decimal
end

So, when we have to know a consumption of an engine of 1950cc, we will provide a parameter 1950:

julia> println(ShowConsumption(1950))
11.47

Great, and what we have to do in case we have to add new functionality: for example, Consumption in winter. What we will do? Sure, we will write a new function ShowConstumptionInWinter(cc), but there's a more clever way to solve a problem: we can use optional arguments for our function.

We will leave original function ShowConsumption and add 1 optional argument, which will be used only in case we want our function calculate something different. Let's take we know that for winter consumption is as twice as big as in summer and it means that function must double normal consumption. It's better to show in on example:

function ShowConsumption(cc,winter=1)
    return round(winter*cc/170*1,2)
end

In this case we can (but not obliged to) provide two arguments to our function (cc and winter), but winter coefficient argument is not compulsory. It means that in case we don't provide winter argument, function will know, that if it is not provided, it's value is by default =1. Let's call it:

julia> println(ShowConsumption(1950))
11.47

julia> println(ShowConsumption(1950,2))
22.94

You see, that first time we provided only one argument (cc) and calculating mechanism inside the function already knew, that value of this "by us not provided" argument is 1. Second time we called a function providing optional argument winter with a value of 2. Providing this optional (winter) argument is actually overiding default value (1).


Example No. 2 : Salary calculation

As you already know, arguments can be of a different data types: integer, string, boolean etc. In previous example we constructed a function, taking integer argument, in this example we will work with boolean argument.

Now imagine we are making a function, which calculates month's salary for an employee regarding of made work shifts (300$ for evey shift), BUT in case an employee has children, he will receive additionally 200$:

function CalculateSalary(shifts,kids=false)
    salary = shifts*300

    if kids == 1
        salary += 200
    end

    return salary
end

In this case function requires two arguments: number of shifts (shifts) and kids (yes or no). But default function "thinks" that employee has no kids:

julia> CalculateSalary(10)
300

julia> CalculateSalary(10,true)
3200

As you, when we don't provide kids argument, function just calculates salary = shifts*300 and by default "thinks" that employee has no kids. In second call we provide kids argument (true), and inside a function we see, that in case kids parameter is true, employee's salary was increased by 200$.