Is it neccessary to define a variable as local if ...


#1

Is it neccessary to define a variable as local if this variable is inside an function ?
What is the utility of “local” in this case ?

fn myFunction =
	(
	local x
	)

“x” is not local by default ?
I ask that because several scripters use this.


#2

If you don’t declare it explicitly, it will first check to see if that variable is already in existence, and if it is it will attempt to use the existing copy. If you declare it explicitly, it will always create a new variable. This allows you to have local variables nested within each other that have the same name but different values.


#3

Ok I see.
Thank you again.


#4

Wait, I didn’t answer your question properly – if you still declare the variable before using it but simply omit the local, then I think this will still behave the same as if you had said local


#5

After your first answer, I wrote a little test:

fn myFunction =
	(
	local x
	x=111
	y=222
	z=333
	format "inside fn : x=%  y=%  z=% 
" x y z
	)


x=1
y=2
format "before fn : x=%  y=%  z=% 
" x y z
myFunction()
format "after fn : x=%  y=%  z=% 
" x y z

first time:

before fn : x=1  y=2  z=undefined 
inside fn : x=111  y=222  z=333 
after fn : x=1  y=2  z=undefined 

second time:

before fn : x=1  y=2  z=undefined 
inside fn : x=111  y=222  z=333 
after fn : x=1  y=222  z=333 

That seems dangerous not to declare a variable as local.
The variable Y has changed after his passage in the function.
And the variable Z is undefined the fist time and not the second time…
And the Y at the second time give an strange result…


#6

Yes, this is why you want to declare local variables used in functions as local.

To explain this, I will show you what MAX Script sees:

 
fn myFunction =
	(
	local x
	x=111
	y=222
	z=333
	format "inside fn : x=%  y=%  z=% 
" x y z
	) 

Define a new function. Make a local variable x. Check to see if variables y and z exist. Since they do not, declare 2 local variables called y and z.


x=1
y=2

Make 2 “local” variables at the global scope (so they are global).


format "before fn : x=%  y=%  z=% 
" x y z

A variable z is attempting to be used without having been declared. Check to see if z exists, it does not…so declare a new global variable for z, leave it undefined. Perform the operation.

myFunction()

Open the function. The local variables x, y, z inside this function enter scope, “shadowing” the global versions. Inside the function, we change these local variables, but then these variables leave scope when we return to the global level, without making any difference to the global variables.


format "after fn : x=%  y=%  z=% 
" x y z

All variables exist as globals, just perform the op.

Now you evaluate again.

fn myFunction =
	(
	local x
	x=111
	y=222
	z=333
	format "inside fn : x=%  y=%  z=% 
" x y z
	)

Replace the definition of myFunction with a new definition. Give it a local variable x. They are trying to use y and z without declaring, so check to see if these variables exist. They do, they exist as global variables, so let this function continue to use those variables.


x=1
y=2
format "before fn : x=%  y=%  z=% 
" x y z

All of these variables exist as global variables, just modify them and perform the op.

myFunction()

Open up the function. It has 1 local variable, x, this will shadow the global variable x. The other global variables y and z are left in scope to be modified by the function.

format "after fn : x=%  y=%  z=% 
" x y z

The global variables have been changed by the function


#7

So for functions it is important, but for instance in a struct, the variables are declared simply by listing them…so you do not need to say “local” for them for them to create local copies


#8

hey thanks a lot for this explanations.

It is interesting for me because I believed that variables inside a function had no influence on the global scope. I believed that only the variables passed by arguments (fn a b c…) were modified. (and the others were considered like local variable) But it is not the case.

It is surprising that I have never had problem before with my scripts.


#9

no problem, I’m glad you brought it up because I have been doing it myself too!!!

you know about passing by reference with & too, right?


#10

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.