Ones more information for education about this topic.
Quiz: Why global variable $c from inside MyFunc without “:return $c ;” still exist ?
.

.
Answer hide here, just select text from here >>
Just because it is :global and it must be save in “/system script environment” !
Means it’s not bug, it’s :global feature !
<< to here.
Source code from screenshot:
system script environment print where name=c;
system script environment remove [find name=c];
system script environment print where name=c;
{
delay 1s; beep;
local a "localA"; put $a;
put "***** enter function *****"
local MyFunc do={set $a "changedA"; put $a; local b "localB"; put $b; global c "globalC"; put $c;system script environment print where name=c;}
put "***** exit function *****"
put "\$a: $a";
put ("\$b: ".$b);
put $"c";
put "***** Run Function: *****"
$MyFunc
put "***** After Function: *****"
put "\$a: $a";
put ("\$b: ".$b);
put $c;
}
system script environment print where name=c;
system script environment remove [find name=c];
system script environment print where name=c;
.
.
And some Fact’s about Function
Situation 1) Just created local/global not work in function
*) If you create a new global or local in script then you cannot use it in Funcrion inside. The global will be created after that you :quit script.
*) Function can use with global who exist in ROS env befor startung your script. Means your script with new global works on second and next runnings. Be aweare that.
.
{ global GlobalVar "SomeValue"
local MyFunc do={put $GlobalVar}
$MyFunc }
.
{ local LocalVar "SomeValue"
local MyFunc do={put $LocalVar}
$MyFunc }
.
Situation 2) Passing arguments to Funcrion
*) New local and global created inside script who was pass to Funcrion as arguments can be used inside, but all modify will be only inside Function itself.
*) global created in ROS env before running script can be access and modify inside Funcrion. Means second and next runnings will be works with global.
{ global GlobalVar "SomeValue"
local MyFunc do={put $1}
$MyFunc $GlobalVar }
.
Situation 3) Receive feedback from Function
*) return can give feedback as only one value from Funcrion. This value can be array with multiple data in it.
*) globals who exit before script start will works correctly
{ local LocalVar "SomeValue"
local MyFunc do={return ($1."AfterChange")}
local FeedBack [$MyFunc $LocalVar]
put $FeedBack }
.
Situation 4) Check if result from :return is not “;” char, it is array..
*) When we not specify what value be :return then by default we receive the “;” chars as function feedback. This is array with :len >=2.
.
Problem is perfect specify this
{ local LocalVar "SpecificValue"
local MyFunc do={ if ($1="SpecificValue") do={return ($1."AfterChange")}}
for i from=1 to=4 do={ set $LocalVar [$MyFunc $LocalVar] ; put $LocalVar } }
Output will be: SpecificValueAfterChange > ; > ; > ;
.
Else={return “”} | Else={return $1} | Else={return [nothing]} as solve
.
{ local LocalVar "SpecificValue"
local MyFunc do={ if ($1="SpecificValue") do={return ($1."AfterChange")} else={return [nothing] }}
for i from=1 to=4 do={ set $LocalVar [$MyFunc $LocalVar] ; put $LocalVar } }
Output will be: SpecificValueAfterChange > “” > “” > “”
.
or just check is feedback it’s not array;
.
{ local LocalVar "SpecificValue"
local MyFunc do={ if ($1="SpecificValue") do={return ($1."AfterChange")}}
for i from=1 to=4 do={ set $LocalVar [$MyFunc $LocalVar] ; if ([typeof $LocalVar]!="array") do={put $LocalVar}}}
.
Output will be: SpecificValueAfterChange