Scrivere una funzione

Julia intermedio

Anthony Markham

Quantitative Developer

Ripasso: struct

  • Le struct sono un’applicazione dei tipi compositi.
  • Sono come altri tipi, ma con tutti i campi che vuoi.
mutable struct House
    bedrooms::Int64
    bathrooms::Int64
    location::String
    price::Float64
end
my_house = House(3, 2, "Sydney", 1500000)
  • Comuni in molti linguaggi per problemi reali.
Julia intermedio

Funzioni e struct personalizzate

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")
Julia intermedio

Ripasso: funzioni

  • Come passare tipi di argomento diversi a una funzione
function arg_types(pos, ; key)
    return pos, key
end
  • Come passare un numero variabile di argomenti a una funzione
function vararg_names(names...)
    return names
end
  • Come restituire l’output da una funzione
function return_x_times_y(x, y)
    return x * y
end
  • Le funzioni sono flessibili e scriverle è essenziale.

  • Risolvere problemi pratici è fondamentale per imparare a programmare.

Julia intermedio

Passons à la pratique !

Julia intermedio

Preparing Video For Download...