Positional and Default Arguments, Type Declarations

Intermediate Julia

Anthony Markham

Quantitative Developer

Function arguments overview

  • When declaring a function, we specify parameters.
function my_function(param1, param2)
    return param1, param2
end
  • When calling a function, we pass arguments in to these parameters.
my_function(1, 2)
(1, 2)
Intermediate Julia

Positional arguments

  • The function arguments that we have seen so far are positional.

  • Positional arguments rely on the order that they are specified in.

function my_function(param1, param2)
    return param1, param2
end
my_function(2, 1)
(2, 1)
Intermediate Julia

Default arguments

  • Default arguments provide a default value if an argument is not specified.
function my_function(param1, param2=2)
    return param1, param2
end
my_function(1)
(1, 2)
Intermediate Julia

Type declarations

  • Type declarations allow us to control the type of data passed into a function.

  • Allows us to safeguard our code against incorrect values passed to functions.

  • Each parameter can have a data type specified. Use double colon :: syntax.

function my_function(param1::String, param2::Integer=2)
    return param1, param2
end
my_function(1)
MethodError: no method matching my_function(::Int64)
Intermediate Julia

Let's practice!

Intermediate Julia

Preparing Video For Download...