撰寫自己的函式

Julia 中級

Anthony Markham

Quantitative Developer

Struct 重點回顧

  • Struct 是複合型別的應用。
  • 類似其他型別,但可包含任意多個欄位。
mutable struct House
    bedrooms::Int64
    bathrooms::Int64
    location::String
    price::Float64
end
my_house = House(3, 2, "Sydney", 1500000)
  • 許多程式語言常用來解決真實世界問題。
Julia 中級

函式與自訂 struct

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 中級

函式重點回顧

  • 如何將不同型別的引數傳入函式
function arg_types(pos, ; key)
    return pos, key
end
  • 如何將可變數量的引數傳入函式
function vararg_names(names...)
    return names
end
  • 如何從函式回傳輸出
function return_x_times_y(x, y)
    return x * y
end
  • 函式很有彈性,自己撰寫函式是程式設計的基本功。

  • 動手解決真實問題是學習寫程式的關鍵。

Julia 中級

一起來練習吧!

Julia 中級

Preparing Video For Download...