Basic Functions

Introduction to Julia

James Fulton

Climate informatics researcher

What are functions?

Functions you have used:

  • println()
  • typeof()
  • string()
  • push!()
  • pop!()
  • append!()
  • length()
  • sort()
x = [2,1,3]


# Takes an array, returns integer l = length(x)
# Takes an array, returns sorted array x_sorted = sort(x)
# Takes a value, prints it to console println(l)
Introduction to Julia

Why use functions?

  • Allows us to focus on program structure
  • Can ignore irrelevant details of how a function works
Introduction to Julia

Writing custom functions

# Declare function to convert temperatures
function fahrenheit2celsius(temp)

# Function body return (temp - 32) * 5/9
end
# Use function println(fahrenheit2celsius(212))
100.0
Introduction to Julia

Writing custom functions

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    return (temp - 32) * 5/9
end

# Use function many times
println(fahrenheit2celsius(212))
println(fahrenheit2celsius(100))
100.0
37.77
Introduction to Julia

Longer functions

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    temp_sub = temp - 32
    temp_c = temp_sub * 5/9
    return temp_c
end

t = fahrenheit2celsius(212) println(t)
100.0
Introduction to Julia

Longer functions

# Declare function to convert temperatures
function fahrenheit2celsius(temp)
    # Function body
    temp_sub = temp - 32       # variable inside function not available outside
    temp_c = temp_sub * 5/9
    return temp_c
end

t = fahrenheit2celsius(212)
println(temp_sub)
ERROR: UndefVarError: temp_sub not defined
Introduction to Julia

Return keyword

function x_or_zero(x)

if x>0 return x else return 0 end
end
println(x_or_zero(-3)) println(x_or_zero(3))
0
3
Introduction to Julia

Return keyword

# Function with longer body
function check_if_raining(rain_amount)
    is_raining = rain_amount > 0
    if is_raining
        println("Better get your coat")
    else
        println("The sky looks clear")
    end
end


check_if_raining(0.2) # Function returns nothing - only prints
Better get your coat
Introduction to Julia

Multiple arguments

function power(x, y)
    return x^y
end

# Use function to calculate 5*5
println(power(5, 2))
25
# Use function to calculate 2*2*2*2*2
println(power(2, 5))
32
Introduction to Julia

Broadcasting functions

function fahrenheit2celsius(temp)
    return (temp - 32) * 5/9
end

temps_f = [212, 32, 100]


# Function not written to work with arrays temps_c = fahrenheit2celsius(temps_f)
ERROR: MethodError: ...
Introduction to Julia

Broadcasting functions

function fahrenheit2celsius(temp)
    return (temp - 32) * 5/9
end

temps_f = [212, 32, 100]

# Broadcast function with dot syntax
temps_c = fahrenheit2celsius.(temps_f)

println(temps_c)
[100.0, 0.0, 37.77]
Introduction to Julia

Broadcasting functions

x = ["one", 2, 3.0]

# Broadcast using typeof function
println(typeof.(x))
[String, Int64, Float64]
Introduction to Julia

Broadcasting multiple arguments

function power(x, y)
    return x^y
end

x_arr = [1,2,3,4,5]


# Square each element of the array
println(power.(x_arr, 2))
[1, 4, 9, 16, 25]
Introduction to Julia

Broadcasting multiple arguments

function power(x, y)
    return x^y
end

x_arr = [1,2,3,4,5]
y_arr = [1,2,3,4,5]

# Use function on x_arr and y_arr
println(power.(x_arr, y_arr))
[1, 4, 27, 256, 3125]
Introduction to Julia

Let's practice!

Introduction to Julia

Preparing Video For Download...