lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/8/12, David Given <dg@cowlark.com> wrote:
> It's much easier to think of this as *all* values being passed by
> reference... it's just that numbers, like strings, are immutable and
> cannot be changed. Therefore:
> a = a + 1
> ...does not *change* the value of the thing a points at. Instead it
> creates a new number with the value (a+1) and then reassigns a to point
> at the new number instead of the old one.

Yes, it is correct.
But is this explanation suitable for a child?

The "value of variable" concept is known to everybody:
1) From math.
When point on plane moves to the right, it increases it's abscissa
value.  Does it mean a new abscissa value is created and assigned to
x?  No!  x simply changes itself.
2) From real life.
A number on a speedometer is a value of variable "speed".  When you
accelerating your car does something inside it create and assign to
speed?  No!  Speed just changes
per se.

This is how all beginners understanding what "variable" is.
That is why all languages designed for educational purposes does not
have passing parameters by reference.  Even long arrays are copied
when passed to procedure or assigned to another variable.

That is why all people are more likely to think of copying numbers in
Lua than duplicating a reference to the same number.  :-)