Julia 中級
Anthony Markham
Quantitative Developer
function my_function(param1, param2)
return param1, param2
end
my_function(1, 2)
(1, 2)
目前為止看到的函式引數都是位置引數。
位置引數依賴指定的順序。
function my_function(param1, param2)
return param1, param2
end
my_function(2, 1)
(2, 1)
function my_function(param1, param2=2)
return param1, param2
end
my_function(1)
(1, 2)
型別宣告可控制傳入函式的資料型別。
可防止把不正確的值傳給函式,保護程式碼。
每個參數都能指定資料型別。使用雙冒號 :: 語法。
function my_function(param1::String, param2::Integer=2)
return param1, param2
end
my_function(1)
MethodError: no method matching my_function(::Int64)
Julia 中級