Writing Your Own Function

Intermediate Julia

Anthony Markham

Quantitative Developer

Structs recap

  • Structs are an application of composite types.
  • These are like other types, but with as many fields as you want.
mutable struct House
    bedrooms::Int64
    bathrooms::Int64
    location::String
    price::Float64
end
my_house = House(3, 2, "Sydney", 1500000)
  • Common in many programming languages for solving real-world problems.
Intermediate Julia

Functions and custom structs

mutable struct Person
    age::Int64
    height::Int64
    location::String

    function Person(age, height)
        new(age, height, "London")
    end
end

steve = Person(19, 180)
Person(19, 180, "London")
Intermediate Julia

Functions recap

  • How to pass different argument types into a function
function arg_types(pos, ; key)
    return pos, key
end
  • how to pass a variable amount of arguments into a function
function vararg_names(names...)
    return names
end
  • how to return output from a function
function return_x_times_y(x, y)
    return x * y
end
  • Functions are flexible and writing your own functions is an essential part of programming.

  • Practical, real-world problem solving is a key part of learning to code.

Intermediate Julia

Let's practice!

Intermediate Julia

Preparing Video For Download...