在Julia中,使用这两种或三种方式创建函数是否有任何区别呢?
- 使用关键字
使用关键字
使用关键字
以下答案是:这三个定义完全等同。 编辑:澄清问题,并强调答案。
- 使用关键字
function
创建函数。function g1(x)
# compute something, store it in `result`
return result # (return keyword is optional)
end
使用关键字
let
。g2(x) = let
# compute something, store it in `result`
return result # (return keyword is optional)
end
使用关键字
begin
g3(x) = begin
# compute something, store it in `result`
return result # (return keyword is optional)
end
以下答案是:这三个定义完全等同。 编辑:澄清问题,并强调答案。
function
(可选的最后返回),和end
,或者用= begin
,没有返回,end
,或者用= let
,没有返回,end
,来创建完全相同的函数。这三种方式是完全等效的。对吗? - undefinedreturn
在let
和begin
块中也是允许的,因为它们位于函数定义内部。 - undefinedreturn
在所有选项中都是允许的,就像在函数范围内的任何地方一样。您可以使用@code_warntype
宏来验证您是否得到相同的结果。 - undefined