1. Can Procedures Modify Variables??
- Posted by Sam <samuelc at teleport.com> Apr 06, 2002
- 378 views
Hi ... Is it possible to pass a variable to a procedure and have the procedure change the value of the variable?? global atom X procedure Add6 (atom X) X = X + 6 end procedure X = 3 print (1, X) puts (1, "\n") Add6 (X) print (1, X) Output: 3 3 Any way to make the output: 3 9 Thanks for all your help, guys. Sam
2. Re: Can Procedures Modify Variables??
- Posted by Derek Parnell <ddparnell at bigpond.com> Apr 06, 2002
- 345 views
----- Original Message ----- From: "Sam" <samuelc at teleport.com> To: "EUforum" <EUforum at topica.com> Subject: Can Procedures Modify Variables?? > > Is it possible to pass a variable to a procedure and have the > procedure change the value of the variable?? > > global atom X > procedure Add6 (atom X) > X = X + 6 > end procedure > X = 3 > print (1, X) > puts (1, "\n") > Add6 (X) > print (1, X) > > Output: 3 > 3 > > Any way to make the output: > > 3 > 9 > > Thanks for all your help, guys. Hi Sam, no you cannot do that in Euphoria. This is because Euphoria only supports 'pass-by-value' for parameters. This means that when you pass a variable as a parameter to a Euphoria routine, the value of that variable is passed, not the variable itself. So even if your routine updates the parameter, the original variable is still untouched. There are three ways that people tend to use to get around this restriction. (1) Use functions instead of procedures. global atom X function Add6 (atom X) return X + 6 end function X = 3 print (1, X) puts (1, "\n") X = Add6 (X) print (1, X) (2) Use variables of bigger scope. That is, variables that can be "seen" by routines but are not defined in the routine. global atom X procedure Add6 () X += 6 end procedure X = 3 print (1, X) puts (1, "\n") Add6 () print (1, X) (3) Use RAM locations. That is really a variation of (2) but allows for more complex data to be effected plus allows the use of parameters. global atom X X = allocate(4) procedure Add6 (atom X) atom temp temp = peek4s(X) temp += 6 poke4(X, temp) end procedure poke4(X,3) print (1, peek4s(X)) puts (1, "\n") Add6 (X) print (1, peek4s(X)) Each has pluses and minuses. There is no one perfect answer. Some people advocate for 'pass-by-reference' to be added to Euphoria. This is where a reference to the variable is passed to the routine rather than its value. So when the routine updates the parameter, it actually refers to the original variable and updates the value in the original variable. This also has issues that can make maintaining programs harder (ie, it is easier to insert bugs). Currently, Euphoria takes the safest route. Even though it can mean you are forced into writing more code than you'd like to and making some programs run slower. ----------- Derek.
3. Re: Can Procedures Modify Variables??
- Posted by Igor Kachan <kinz at peterlink.ru> Apr 06, 2002
- 337 views
Hi Sam, ---------- > ïÔ: Sam <samuelc at teleport.com> > ëÏÍÕ: EUforum <EUforum at topica.com> > ôÅÍÁ: Can Procedures Modify Variables?? > äÁÔÁ: Saturday, April 06, 2002 17:58 > > Hi ... > > Is it possible to pass a variable to a procedure and have the > procedure change the value of the variable?? > > global atom X > procedure Add6 (atom X) > X = X + 6 > end procedure > X = 3 > print (1, X) > puts (1, "\n") > Add6 (X) > print (1, X) > > Output: 3 > 3 > > Any way to make the output: > > 3 > 9 > > Thanks for all your help, guys. > > Sam --- X.e global atom X procedure Add6() X += 6 -- this X is that X above, *only* end procedure X = 3 -- this X is that X above print(1, X) -- this X is that X above puts(1, "\n") Add6() print(1, X) -- this X is that X above puts(1, "\n") function ADD6(atom X) -- this X is not that X above, *any* return X+6 -- this X is this X that is not that above end function ? X -- this X is that X above X=ADD6(X) -- both Xs are that X above ? X -- this X is that X above -- end of X.e Regards, Igor Kachan kinz at peterlink.ru
4. Re: Can Procedures Modify Variables??
- Posted by Kat <gertie at PELL.NET> Apr 06, 2002
- 355 views
On 6 Apr 2002, at 13:58, Sam wrote: > > Hi ... > > Is it possible to pass a variable to a procedure and have the > procedure change the value of the variable?? Yes, check out the word "function"..... > global atom X > function Add6 (atom X) > X = X + 6 return x > end procedure > X = 3 > print (1, X) > puts (1, "\n") x = Add6 (X) > print (1, X) > > Output: 3 9 > Thanks for all your help, guys. And women. Kat, meow