Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Return of a function

 [1/10] from: info:id-net:ch at: 18-Apr-2002 9:21


Hi, I really need to understand why the return of this function doesn't work. transfo-time: func [hm] [ hm: parse hm "." hm/1: to-integer hm/1 hm/2: to-integer hm/2 hm: to-time join hm/1 [":" hm/2] hm ] heure: "12.01" transo-time heure heure: hm print heure; nothing is print.. heure has no value now.. why ??? Philippe

 [2/10] from: sunandadh:aol at: 19-Apr-2002 4:24


Phillippe:
> print heure; nothing is print.. heure has no value now.. why ???
I see two main problems: 1. You define transFo-time yet use transo-time (no F in name) 2. You use the hm variable. But that's local to the function. Try this: transfo-time: func [hm] [ hm: parse hm "." hm/1: to-integer hm/1 hm/2: to-integer hm/2 hm: to-time join hm/1 [":" hm/2] hm ] heure: "12.01" heure: transfo-time heure print heure ;; will print 12:01 Inevitably loads of people will rewrite your function in clever and elegant ways. Here's my one-liner version. This will also work for 12.01.02: transfo-time: func [hm] [ return to-time replace/all hm "." ":" ] (I know I don't need 'return, but I like to be explicit about these things) Sunanda.

 [3/10] from: info:id-net:ch at: 18-Apr-2002 10:58


Why hm is local to function ? There's no /local refinement + The hm is called outside. So in my understanding, hm shouldn't be local.. So what I have to do to have a global variable ?? why do u write heure: transfo-time heure.. ? Philippe

 [4/10] from: lmecir:mbox:vol:cz at: 19-Apr-2002 11:11


Hi Philippe, try instead: heure: "12.01" heure: transfo-time heure ----- Original Message ----- Hi, I really need to understand why the return of this function doesn't work. transfo-time: func [hm] [ hm: parse hm "." hm/1: to-integer hm/1 hm/2: to-integer hm/2 hm: to-time join hm/1 [":" hm/2] hm ] heure: "12.01" transo-time heure heure: hm print heure; nothing is print.. heure has no value now.. why ???

 [5/10] from: info:id-net:ch at: 18-Apr-2002 11:22


when I write heure: transfo-time heure.. the variable is grabbed by the variable, right ?

 [6/10] from: sunandadh:aol at: 19-Apr-2002 6:09


Philippe:
> Why hm is local to function ? There's no /local refinement + The hm is > called outside. So in my understanding, hm shouldn't be local.. > So what I have to do to have a global variable ??
Hm is the _local _name for the _value_ (not, in this case, variable) passed in. What is returned is a _value_ not a variable. Try this: test-func: func [value] [ value: value + 200 return value ] print test-func 100 print value the first 'print correctly prints 300. The second fails as the word 'value only has a meaning within the function. If you want a global variable 'value too, then: value: test-func 100 print value ;; prints the global 'value, ie 300 another-value: test-func 500 print another-value ;; prints 700 print value ;; prints 300. Changing 'value in the func doesn't affect the global variable Sunanda.

 [7/10] from: info:id-net:ch at: 18-Apr-2002 13:11


Thanks a lot, Sunanda. I never saw this rebol' particularity before. Phil

 [8/10] from: lmecir:mbox:vol:cz at: 19-Apr-2002 13:28


Hi Philippe, the 'hm variable is local to the function, because it is the name of an argument of the TRANSFO-TIME function. (arguments are local) If you want to retain the returned value, you should store it somewhere. The following would work too: hm: transfo-time heure , but the 'hm word is not the same word as the 'hm word used in the function in this case. ----- Original Message ----- when I write heure: transfo-time heure.. the variable is grabbed by the variable, right ?

 [9/10] from: ingo::2b1::de at: 19-Apr-2002 13:11


Hi Philippe, Philippe Oehler wrote:
> Why hm is local to function ? There's no /local refinement + The hm is > called outside. So in my understanding, hm shouldn't be local..
_All_ arguments to functions are local to that function. Think about it: do you really want all arguments to functions be defined in the global context? hm is not _called_ outside the function, it only gets its _value_ from outside.
> So what I have to do to have a global variable ??
In Rebol, by default, everything is global, except when explicitly noted. So, for you, just just change it like this: transfo-time: func [hm-arg] [ hm: parse hm-arg "." hm/1: to-integer hm/1 hm/2: to-integer hm/2 hm: to-time join hm/1 [":" hm/2] hm ] Now hm is not explicitly listed as local, and thus it's global. (A word to the wise, I would _not_ recommend to program it this way.)
> why do u write heure: transfo-time heure.. ?
transfo-time returns the last value, which is hm. Now Sunanda sets the word heure - which held the time string in the beginning - to the value returned by transfo-time, which is the computed time value. I hope that helps, Ingo

 [10/10] from: joel:neely:fedex at: 19-Apr-2002 6:58


Hi, Philippe, Philippe Oehler wrote:
> Hi, > > I really need to understand why the return of this function > doesn't work. >
The function *does* return a value (if, as Sunanda points out, you call it by its actual name ;-)... Cutting and pasting from your email:
>> transfo-time: func [hm] [
[ [ hm: parse hm "." [ hm/1: to-integer hm/1 [ hm/2: to-integer hm/2 [ hm: to-time join hm/1 [":" hm/2] [ hm [ ]
>> >> heure: "12.01"
== "12.01"
>> >> transo-time heure
** Script Error: transo-time has no value ** Near: transo-time heure Calling with the actual name:
>> transfo-time heure
== 12:01 But the rest of your question isn't about a return value:
> heure: hm > print heure; nothing is print.. heure has no value now.. why ??? >
You seem to expect that a global word HM would be set by the evaluation of TRANSFO-TIME but that won't happen. Your definition of TRANSFO-TIME begins with transfo-time: func [hm] [ which means that the word HM within the function body is local to the function because it is the argument to the function. With all that said, the function itself is a bit puzzling to me. If you really expect a string which represents a time (but with a dot instead of the colon that REBOL expects) then the conversions (from string to block, block of strings to block of integers, integers back to string) are unnecessary overhead. Why not say transf-time: func [s] [ to-time replace s "." ":" ] which does this:
>> transf-time "12.01"
== 12:01
>> type? transf-time "12.34"
== time! -jn- -- ; Joel Neely joeldotneelyatfedexdotcom REBOL [] do [ do func [s] [ foreach [a b] s [prin b] ] sort/skip do function [s] [t] [ t: "" foreach [a b] s [repend t [b a]] t ] { | e s m!zauafBpcvekexEohthjJakwLrngohOqrlryRnsctdtiub} 2 ]