how are variables passed from functions?

Mike Bonner bonnmike at gmail.com
Fri Nov 14 12:40:35 EST 2014


For anything other than this simple case, the returns matter.

Take for example

local tvar1,tvar2,tvar3
on mouseUp
--get a random value to use
   put random(10) into tvar1
   put "Start Value: " & tvar1 & cr & "Final Return:" &  myfunc1(tvar1) &
cr & tvar3
end mouseUp

function myfunc1 pvar
   put 2 * myfunc2(pvar) into tVar2
   return tvar2

end myfunc1

function myfunc2 pvar
   put pvar * 2 into tvar3
   return tvar3
end myfunc2

With this function, if the random number is 10,  the value returned from
the function is 40, doubled twice once by each function.
the double in the 3rd function happens PRIOR to the execution of the second
function (the end of the chain is first working backwards)
So tvar3 (in the last function) is the first double, so the value in tvar3
will be 20.  Then the other double is done and placed into tvar2. So the
value returned will be 40.

If you remove the return from myfunc1, the final doubling isn't returned.
The value of tvar2 IS 40, but its never returned, so you end up with an
unanticipated return, the return (which as discovered earlier) is "the
result" from the whole chain.  Since the only actual result is from
muFunc2, that is the value that ends up being returned to the original
function call.

On Fri, Nov 14, 2014 at 10:02 AM, Jacques Hausser <jacques.hausser at unil.ch>
wrote:

> Yes, it is ’the Result’, I just checked it - good hint, Dave ! didn’t
> think about that before…
>
> Jacques
>
> > Le 14 nov. 2014 à 17:56, Dave Cragg <dave.cragg at lacscentre.co.uk> a
> écrit :
> >
> > Tiemo,
> >
> > Interesting. It looks like functions return the value of "the result" in
> the absence of a return statement.
> >
> > Other languages would probably return "void" or "null" or something
> similar, and we might expect Livecode to return "empty".
> >
> > I've no opinion on whether it is a bug or not, but I think it is
> probably better to specifically return a value from a function. We don't
> always have control over what appears in "the result".
> >
> > Cheers
> > Dave
> >
> >
> >
> > On 14 Nov 2014, at 08:47, Tiemo Hollmann TB <toolbook at kestner.de> wrote:
> >
> >> By accident I noticed that I don't have a return at a certain point of a
> >> function but nevertheless the value was passed back without a return to
> the
> >> calling handler.
> >>
> >> I can reproduce the issue to this scenario:
> >>
> >>
> >>
> >> --Btn script
> >>
> >> on mouseUp
> >>
> >>   put foo1() into myVar1 -- I get the value "Test"
> >>
> >> end mouseUp
> >>
> >>
> >>
> >> --script in stack
> >>
> >> function foo1
> >>
> >>  put foo2() into myVar2 -- no return in this function
> >>
> >> end foo1
> >>
> >>
> >>
> >> function foo2
> >>
> >>  return "Test" -- return here in second level
> >>
> >> end foo2
> >>
> >>
> >>
> >> Up to now I thought that every function has to have a return statement
> if
> >> you want anything getting back.
> >>
> >> Can somebody explain to me what is going on here? How is the value from
> >> function foo2 passed back to the mouseup handler, even with different
> var
> >> names?
> >>
> >> Is this a bug or just an accident or even wanted?
> >>
> >> Thanks for enlightening
> >>
> >> Tiemo
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list