This post is about how to define and use functions in Julia. A function is a block of codes that performs a specific operation. It may accept parameters which may influence its behavior. It may also return values to codes calling the function.
How To Define Functions
We define functions in Julia using the function and end keywords. Consider the following codes.
1 2 3 | function my_sum(x::Int, y::Int)::Int return x + y end |
The function name is my_sum. It accepts two parameters and returns a value of type Int8. Alternatively, we can define a compact function. Consider the following codes that are equivalent to the previous codes.
1 | my_sum(x, y) = x + y |
If we do not specify the data type of parameters and return value, we can pass any values. Julia determines the type dynamically during runtime. Consider the following Julia codes that define the anytype function.
1 | anytype(param) = param |
The following codes are equivalent.
1 | anytype(param::Any)::Any = param |
We get the following output in Julia when we use the function with different values.
1 2 3 4 5 6 7 8 9 10 11 | julia> anytype(1) 1 julia> anytype("turreta.com") "turreta.com" julia> anytype(["www", "turreta",".com"]) 3-element Array{String,1}: "www" "turreta" ".com" |
The first example passes a numeric value and gets the same amount. Next, the second example gives a String value and returns the equal value. Lastly, the third example passes an array of String values and gets the same value.
We can also define functions in Julia that don’t accept any parameters and returns nothing. Consider these codes.
1 2 3 4 5 6 7 | julia> function func_noparam_noreturn() println("This function accepts and returns nothing!") end func_noparam_noreturn (generic function with 1 method) julia> func_noparam_noreturn() This function accepts and returns nothing! |
We can also define a function that accepts zero or multiple parameters using the ... operator after a parameter name.
1 2 3 4 5 6 7 8 9 10 | julia> function fn_vararg(names::String...) println(names) end fn_vararg (generic function with 1 method) julia> fn_vararg("Karl", "Sam", "Billy") ("Karl", "Sam", "Billy") julia> fn_vararg("A", "B") ("A", "B") |
A function in Julia does not need a name, and we can define an anonymous function.
1 2 3 4 5 | julia> my_func = (x,y) -> x * y #3 (generic function with 1 method) julia> my_func(3, 3) 9 |
How To Use Functions
Before we can use a function, we must define it first. Meaning, the function definition comes first before its usage. Otherwise, we get an UndefVarError error. Consider the following codes.
1 2 3 4 5 | # No definition of julia> my_product(2, 4) ERROR: UndefVarError: my_product not defined Stacktrace: [1] top-level scope at REPL[33]:1 |
To fix this, we need to define the function before using it.
1 2 3 4 5 6 7 | julia> function my_product(x::Int, y::Int)::Int return x * y end my_product (generic function with 2 methods) julia> my_product(3, 4) 12 |
If we want to make this function compact, we could define it as follows.
1 2 3 4 5 | julia> my_product(x, y) = x * y my_product (generic function with 1 method) julia> my_product(3,4) 12 |
Defining functions is also the way to “implement” interfaces in Julia.