编写你自己的函数

Julia 中级

Anthony Markham

Quantitative Developer

结构体回顾

  • 结构体是复合类型的一种应用。
  • 它们类似其他类型,但可包含任意数量的字段。
mutable struct House
    bedrooms::Int64
    bathrooms::Int64
    location::String
    price::Float64
end
my_house = House(3, 2, "Sydney", 1500000)
  • 在许多编程语言中常见,用于解决实际问题。
Julia 中级

函数与自定义结构体

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

Passons à la pratique !

Julia 中级

Preparing Video For Download...