Sage-Code Laboratory

Julia Functions

A function is a named block of code designed to resolve a small calculation and to be reused. Functions usually have one or more input parameters and should return one single result. Julia is a functional language so we have a lot to talk about functions. Let's get started.

Function Principle

Function Declaration

You can declare a function in Julia using keyword function. The function block is closed with keyword end. Julia syntax is like Pascal. So Julia is not a curly bracket language. It is not like C, Java and JavaScript and this is a good thing. Curly brackets are used for other things.


# In this function we use $name like in Perl
function say_hello(name)
   println("Hi $name, it's great to see you!")

Note:As you can see in the previous example we do not use any symbol after parameter list. In Python and Level 123 we use ":". In Ada and PL/SQL we use "is". But Julia simplify the syntax to minimum. Also this function do not return any result but has a side-effect.

Function Result

The last expression value in a function is the result of the function. Easy to use and very elegant no? What if the things are more complex. Can we create multiple exit points? Yes we can using the return keyword. This will skip all other statements and create an exit point before the end. We can use many return statements.

Function Call

To call a function we use function name followed by (). For example we can cal "say_hello" function using notation: say_hello("Julia"). This will print to console the message "Hi Julia, it’s great to see you!". The notation: $name in string is a place-holder that is replaced by "Julia" string.


# In this function we calculate a math x operator y
function math(x, y, operator)
    if operator=="+"
        return x + y
    elseif operator=="*"
        return x * y
    elseif operator=="/"
        return x / y
        return 0

Variable Scope

A variable can have a global scope or local scope. A variable that is defined outside of any function is a global variable. A local scope is defined for every function. If the functions are nested, the local function can see the variables in the parent scope. Also a function can have internal loops and other blocks that can have a nested soft local scope.

We can force local scope or global scope variables using keyword: "local" or "global" similar to Python. So in the next example the x is local and the y is global. To avoid any confusion we should use different variable names inside the function. If we do'nt declare local or global all new variables defined inside the block are local by default.

# declare 3 global variables
x = 0
y = 0
a = 1

#in test function a and x are local
function test()
    a = 2
    local x
    x = 12
    global y
    y = 12

#funtion test has side-effects
println("x = $x")
println("y = $y")
println("a = $a")

Note: Julia is using hoisting technique. This technique enable you to use a variable and declare the variable later in the code. That means the location of declaration: local or global has no importance, it has the same effect if you use it at beginning or at the end in the block.

Homework: Open previous example on-line and run it: julia-global

Read next: Modules