如何将值从嵌套函数返回朱莉娅的主脚本?
例如:
函数main()
function doit()
A = ones(Int, 2)
return A
end
doit()
display(A)
A = [1, 2, 3]
display(A)
doit()
display(A)
另外,如果嵌套函数具有两三层,我们如何将局部变量值返回MAIN或将其保存到MAIN中?
For example:
function main()
function doit()
A = ones(Int, 2)
return A
end
doit()
display(A)
A = [1, 2, 3]
display(A)
doit()
display(A)
Also what if the nested function has two or three layers, how can we return local variable values into main or save them into main?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引入HAR的功能,因此在外面没有看到通常在其中定义的变量。
就您的情况而言,我知道您希望函数
doit()
具有突变全局变量的外部效果。虽然不建议使用全局变量,并且这种副作用通常不是优雅的模式,但可以通过使用
global
关键字来轻松实现:在这种情况下,您的Julia会话可能看起来像:
有关更多信息,您可以使用现在需要看看朱莉娅(Julia)中的可变范围工作: https:/ /docs.julialang.org/en/v1/manual/variobles-and-scoping/
Functions in introduce ha hard scope so normally variables defined within them are not seen outside.
For your case I understand that you want the function
doit()
have an external effect of mutating a global variable.While the use of global variables is discouraged and such side effects are generally not elegant pattern, it can be easily achieved by using
global
keyword:in that case your Julia session could look like:
For more information, you need to have a look now variable scoping works in Julia: https://docs.julialang.org/en/v1/manual/variables-and-scoping/