Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > can a subroutine alter a variable?
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 1st May 06:05
mark anderson
External User
 
Posts: 1
Default can a subroutine alter a variable?



[using OSX - 10.2.8]

Sorry if I've misunderstood...

I'm getting confused. I'd like to pass a Boolean (initialised as false) as
on of my routine parameters - the name will vary per call:

set sFu to {"w","x"}
set sBar to "xyz"
set blnA to false
StringCheck(sFu,sBar,blnA)
display dialog blnA

set sSna to {"*","#"}
set sFu to "abcd"
set blnB to false
StringCheck(sSna,sFu,blnB)
display dialog blnB

on StringCheck(sArgA, sArgB, blnState)
repeat with sChar in sArgA
if sArgB contains sChar then
set blnState to true
end if
end repeat
-- return blnState
end StringCheck

This routine inherits all 3 variables but doesn't change, for example, blnA
to 'true' as I'd expect. I can ********ly "return blnState" and then set
blnA (or whatever) to than value but that seems very long-winded. I've also
tried setting "property blnState : false" at the head of the script but I
still can't get a 'true' return except via setting the main script to the
routine's 'return' value

It also begs the question about whether sub-routines can change the value of
variables passed from a higher scope. Or are 'Boolean's a special case. If
I was setting 2 Booleans in the sub-routine, how would I retrieve their
values as I understand writing (in the above)

end repeat
return blnStateOne
return blnStateTwo
end StringCheck

would result in the 'return' value of the routine being the value of
blnStateTwo. No?

On the wider horizon, I've learn a lot from the PDF of the advance sample
chapter of O'Reilly's "AppleScript - The Definitive Guide". The sample
chapter just happens to be on variables! It doesn't help with the above
though - as far as I can figure it out. (However, based on what I've read
I'm going to get a copy when it comes out sometime this month - Nov 03)

TIA

Mark
  Reply With Quote


  sponsored links


2 1st May 06:05
jerry kindall
External User
 
Posts: 1
Default can a subroutine alter a variable?



Parameter passing in AppleScript is done by value, not by reference, so
you can't change the variables passed in. The correct way to send a
value back is using the return statement, as in your commented-out
line.

You could also use a global variable, but that is generally considered
a bad way to do things like this. Global variables are for values that
are needed by a lot of your handlers, so many that it would be a pain
to pass it to every one of them. Your global variables should be few
in number, ideally zero but perhaps one or two if you have a hundred or
so other variables that aren't globals. --
Jerry Kindall, Seattle, WA <http://www.jerrykindall.com/>

Send only plain text messages under 32K to the Reply-To address.
This mailbox is filtered aggressively to thwart spam and viruses.
  Reply With Quote
3 1st May 06:05
michelle steiner
External User
 
Posts: 1
Default can a subroutine alter a variable?


blnState is local; it is not changed unless it is ********ly changed.

The way to do it is

set blnA to StringCheck(sFu,sBar,blnA)

Then in the subroutine return blnState

set {blnA,BlnB} to StringCheck(sFu,sBar,blnA,blnB)

And in the subroutine,
return {blnStateOne,blnStateTwo}

--
Never play strip tarot.
  Reply With Quote
4 1st May 06:05
mark anderson
External User
 
Posts: 1
Default can a subroutine alter a variable?


Michelle,


And in the subroutine,
return {blnStateOne,blnStateTwo}
<<<

Ah, of course! Thanks.
  Reply With Quote
5 1st May 06:05
mark anderson
External User
 
Posts: 1
Default can a subroutine alter a variable?


Thanks, with Michelle's separate comment about a list for returning >1
value, I'm back on track

Mark
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666