Confusion with arrays and functions

Hello,

You may find this page helpful: Scope of Variables · The Julia Language.

The compiler does not error because the variables (data1-8) are in the global scope (Main.Data1,…) even when you don’t pass them as argument.

The return value of a function is the return value of the last expression by default. Unless you have an explicit return ....

While ChangeData1(i) and ChangeData3(Data1 #not 3,i) will mutate Data1 in the same way. When Data1 is an argument, the julia compiler produces more efficient code. So a “better practice” is passing in an argument. However, it is not needed, and may not matter for non performance critical code.

Hope this helps.

2 Likes