Undestanding var on subroutines

Strange question: I don’t undestand how variables works on subroutines.
For examples, take a look:

Return of var is “1234”. Why?
In the subroutine (on process) I set var to “5678”, but why when the subroutine ends tha var don’t remain unchanged?
Maybe the variables can exists only inside the routines? No, because inside on process var is “1234”, before to become “5678”: So why?

And how can I make a total variable for ALL subroutine?

Tnx for the answare

Hi,

local variables work only within a single handler.
Normal defined variables are always local variables.
To use a variable globally, declare it as global or use a property

For declaring a global variable, just write

global var

But your script doesn’t work either with a global variable,
because the first var won’t be altered after the process handler
unless you set it again.

This works also without a global variable


on run
    set var to "1234"
    set var to process(var)
    return var
end run

on process(var)
    set var to "5678"
end process

I undestand. Your script works fine (as all your script, of course :), but there is some other little question about variables.
For example:

on run
	set var to "1234"
	set bar to "albero"
	set var to process(var)
	return var
end run

on process(var)
	set var to "5678"
	set bar to "palma"
end process

Why return of var is “palma”?
The declaration

set var to process(var)

intend that only the final variable goes to “var”? And, if i need more than a variable? If I need “var” and “bar” (no more fantasy, I know)?
This script give me strange return

on run
	set var to "1234"
	set bar to "albero"
	set {var, bar} to process(var, bar)
	return var
end run

on process(var, bar)
	set var to "5678"
	set bar to "palma2"
end process

The return is “p”. What the hell is going to Applescript engine to return me “p”???

I answer myself! :smiley:

Here the final script

on run
	set var to "1234"
	set bar to "albero"
	process()
	return bar
end run

on process()
	global var
	set var to "5678"
	
	global bar
	set bar to "palma"
end process

I have to call subhandler without variable, and declare them global inside the subhandler. It works fine!!! Incredible.

Adobe, I’m coming :stuck_out_tongue:

The handler process returns the result of the last statement
unless you write e.g.

on process(var)
	set var to "5678"
	set bar to "palma"
	return var
end process

if you want to get back two values, you must also return (a list of) two values
In your example you return {“palma”}, so AppleScript takes the string as list and assigns the first two characters to the variables
(I’m sure, bar returns “a” ;))

that does it

on process(var, bar)
	set var to "5678"
	set bar to "palma2"
	return {var, bar}
end process

Parameter variables are implicitly local. Try compiling this:

on run
set var to “1234”
process(var)
return var
end run

on process(var)
global var
set var to “5678”
end process

Also, in this:

on run
set var to “1234”
set bar to “albero”
set {var, bar} to process(var, bar)
return var
end run

on process(var, bar)
set var to “5678”
set bar to “palma2”
end process

you have no return statement in the subroutine. The subroutine returns the last result (i.e. “palma2”. Then:

set {var, bar} to “palma2”
return {var, bar}

sets var and bar to items 1 and 2 of the string.

Try this:

set {var, bar, a, b, c, d, e} to “palma2”
{var, bar}

Thanks to all. Knowloedge on variables is more then I thinked.

I’ve read this one by Apple
http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.ed.html

Now I’ve undestand that all variables are always local: I can set a variable to Global by type:

global x
set x to "hello"

and this one will be “hello” in run Hendler and in all minor hendler. But, if i set a variable on a minor handler, this one will not exists in other handler, only in run.

on run
	set x to "hi"
	process_item()
	display dialog x -- display "hello"
	process_sub()
	display dialog x -- display "hello", not "Again" as I thinked
end run

on process_item()
	global x
	set x to "hello"
end process_item

on process_sub()
	set x to "Again"
end process_sub

so I have to repeat global set for all minor handler

on run
	set x to "hi"
	process_item()
	display dialog x -- display "hello"
	process_sub()
	display dialog x -- display "Again"
end run

on process_item()
	global x
	set x to "hello"
end process_item

on process_sub()
	global x -- Add now!
	set x to "Again"
end process_sub

Instead of global var, I can use Property in the top of the script, and set them with a random value (and re-set right later)

property x : "random" -- every value will be right here

on run
	set x to "hi"
	process_item()
	display dialog x -- display "hello"
	process_sub()
	display dialog x -- display "Again"
end run

on process_item()
	set x to "hello"
end process_item

on process_sub()
	set x to "Again"
end process_sub

So, I think that’s can be all. Not so easy that I thinked, not impossible as appears some time ago :slight_smile:
Happy to hear some other addictions

Regards