Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function/variable scope (pass by value or reference?)

I'm completely confused by Lua's variable scoping and function argument passing (value or reference).

See the code below:

local a = 9        -- since it's define local, should not have func scope local t = {4,6}    -- since it's define local, should not have func scope  function moda(a)   a = 10           -- creates a global var? end function modt(t)   t[1] = 7         -- create a global var?   t[2] = 8 end  moda(a) modt(t) print(a)  -- print 9 (function does not modify the parent variable) print(t[1]..t[2])  -- print 78 (some how modt is modifying the parent t var)  

As such, this behavior completely confuses me.

  • Does this mean that table variables are passed to the function by reference and not value?

  • How is the global variable creation conflicting with the already define local variable?

    • Why is modt able to modify the table yet moda is not able to modify the a variable?
like image 893
frooyo Avatar asked May 25 '11 17:05

frooyo


People also ask

What is pass by value or pass by reference?

"Passing by value" means that you pass the actual value of the variable into the function. So, in your example, it would pass the value 9. "Passing by reference" means that you pass the variable itself into the function (not just the value).

What is a function pass by reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.

Is JavaScript function pass by reference or value?

JavaScript is always pass-by-value. This means everything in JavaScript is a value type and function arguments are always passed by value. That being said, object types are a bit more confusing. The confusion lies in the fact that object types are reference types which are passed by value.

How is reference variable used for pass by reference?

How Does Pass By Reference Work With Functions? If you recall, using pass by reference allows us to effectively “pass” the reference of a variable in the calling function to whatever is in the function being called. The called function gets the ability to modify the value of the argument by passing in its reference.


1 Answers

You guessed right, table variables are passed by reference. Citing Lua 5.1 Reference Manual:

There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table. ....

Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

So nil, booleans, numbers and strings are passed by value. This exactly explains the behavior you observe.

like image 97
Bas Bossink Avatar answered Sep 18 '22 11:09

Bas Bossink