how are variables passed from functions?

dunbarx at aol.com dunbarx at aol.com
Fri Nov 14 12:34:07 EST 2014


I am a native speaker, but not a native listener.


Yes, it makes no difference. Stepping through, "myVar2" will contain a value regardless of whether a return is present or not.


Personally, I would not use the return there. Even more personally, why have two functions? Unless a choice is made in the primary, control passes along one pathway. Is that what you intended:

function foo1 var
   if var = 3 then put foo2() into myVar2 else return  var
end foo1


Craig





-----Original Message-----
From: Tiemo Hollmann TB <toolbook at kestner.de>
To: 'How to use LiveCode' <use-livecode at lists.runrev.com>
Sent: Fri, Nov 14, 2014 11:21 am
Subject: AW: how are variables passed from functions?


Hi Craig,
perhaps I didn't expressed myself correct, I'm not a native speaker.
For me the result seems the same, if I put a return myVar2 at the end of 
function foo1 or not (see below). The intermediate function is terminated AFTER 
the call of the second function at the end.
My original script works without a return in foo1. My question was, if it is a 
good or bad practice if I would add this return, just for "safety reasons" to be 
sure anything is returned in case I would change anything, e.g. I would replace 
the call of function foo2 by any other structure.
Tiemo

--Btn script
on mouseUp
    put foo1() into myVar1 
end mouseUp

--script in stack
function foo1
   put foo2() into myVar2 
   return myVar2 -- return statement yes or no?
end foo1

function foo2
   return "Test"
end foo2

> -----Ursprüngliche Nachricht-----
> Von: use-livecode [mailto:use-livecode-bounces at lists.runrev.com] Im Auftrag
> von dunbarx at aol.com
> Gesendet: Freitag, 14. November 2014 17:00
> An: use-livecode at lists.runrev.com
> Betreff: Re: how are variables passed from functions?
> 
> Hi.
> 
> 
> Putting intermediate returns will immediately terminate the function call. How
> would you reach the next stage?
> 
> 
> Perhaps this is another "slightly off" aspect of the original example: The
> intermediate variable "myVar2" is only seen in the debugger, and therefore
> only actually "present" at all, when the secondary function returns it s
> value. And even then it is only in passing, you might say, on the way back to
> the primary function call. But this would never complete if you placed a
> return in the primary function handler.
> 
> 
> Try it. Watch the flow. You HAVE to not return in any of these intermediate
> handlers.
> 
> 
> Craig
> 
> 
> 
> -----Original Message-----
> From: Tiemo Hollmann TB <toolbook at kestner.de>
> To: 'How to use LiveCode' <use-livecode at lists.runrev.com>
> Sent: Fri, Nov 14, 2014 10:38 am
> Subject: AW: how are variables passed from functions?
> 
> 
> Hi Craig,
> I wasn't aware of this behaviour.
> I think about more complex functions with a lot of if and else structures,
> where you are perhaps not aware anymore of this function chain after some
> years, when you are changing the first or the second function?
> Would it be a good or at least not a bad practice to put a return in the
> intermediate function? Or could it even break anything if you would explicitly
> return myVar2 in the first function?
> Thanks
> Tiemo
> 
> 
> 
> > -----Ursprüngliche Nachricht-----
> > Von: use-livecode [mailto:use-livecode-bounces at lists.runrev.com] Im
> Auftrag
> > von dunbarx at aol.com
> > Gesendet: Freitag, 14. November 2014 16:12
> > An: use-livecode at lists.runrev.com
> > Betreff: Re: how are variables passed from functions?
> >
> > I don't see any anomaly here. Since the intermediate function calls
> > yet another function, it never needs to "close", that is, return, a
> > value. It
> is a
> > pass-through entity, a card carrying member of the chain of handler calls.
> > Step through this variation:
> >
> >
> >
> > on mouseUp
> >     put double(2) into myVar1
> > answer myVar1
> > end mouseUp
> >
> >
> > function double var
> >    put redouble(var * 2) into myVar2 -- no return in this function end
> > double
> >
> >
> > function redouble var
> >    return var * 2
> > end redouble
> >
> >
> > The "8" you get is processed sequentially through the handler path,
> > just
> like
> > bacon.
> >
> >
> > Craig Newman
> >
> >
> >
> > -----Original Message-----
> > From: Tiemo Hollmann TB <toolbook at kestner.de>
> > To: 'How to use LiveCode' <use-livecode at lists.runrev.com>
> > Sent: Fri, Nov 14, 2014 3:47 am
> > Subject: how are variables passed from functions?
> >
> >
> > 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
> >
> >
> > _______________________________________________
> > 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
> 
> 
> _______________________________________________
> 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
> 
> 
> _______________________________________________
> 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


_______________________________________________
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