I was more asking, can you actually do something with the returned value, or does it only put it on the console? Or in the case of a function, will it return to the main script without executing the rest of the called function?
Yes of course you can use returned value anyway you like. for example
[admin@x86] > :global sum do={ :return ($1 + $2)}
[admin@x86] > :put (14 - [$sum 3 4])
7
Having it be “simple” to write bad code is one thing… having it be unavoidable is another. And with ROS scripting, it’s simple to think of scenarios where bad code is unavoidable.
I create function , and can’t retrieve datas
[misha@test] > ip route print where 172.16.2.1 in dst-address dynamic
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme,
B - blackhole, U - unreachable, P - prohibit
DST-ADDRESS PREF-SRC GATEWAY DISTANCE
74 ADo 172.16.2.0/29 172.16.88.1 110when i convert this command to function, it not working
[misha@test] > :global route do={/ip route print where $1 in dst-address dynamic }
[misha@test] > $route 172.16.2.1
Flags: X - disabled, A - active, D - dynamic, C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme,
B - blackhole, U - unreachable, P - prohibit
If you run this, you can see that the passed parameter type is “str” (string) and needs to be converted to an IP.
:global route do={:put [:typeof $1]; /ip route print where $1 in dst-address dynamic}
$route 172.16.2.1You can do it this way when you call the function:
:global route do={/ip route print where $1 in dst-address dynamic}
$route [:toip 172.16.2.1]Or put the code in the function itself:
:global route do={/ip route print where [:toip $1] in dst-address dynamic}
$route 172.16.2.1
simply check inside the function if the parameter are passed with “:typeof $3”, if the results are “nothing” the parameters are not provided, in this way you can create one function that produce different outputs based on parameters number.
In case someone finds this useful, I’ve added this code to my start-up script to automatically define functions from system scripts that start with “Function” (or any other prefix that you may choose). Just creating a script called “FunctionWhatever” will automatically define a global function with the same name available from terminal session or from any other script (remember to declare the function in the script that will use it, though).
Enjoy!
:local fnArray;
:foreach f in=[/system script find where name~"^Function.*"] do={:set fnArray ($fnArray.",".[/system script get $f name])};
:set fnArray [:toarray $fnArray];
:foreach f in=$fnArray do={:exec script=":global \"$f\" [:parse [/system script get $f source]]"; /log info ("Defined function ".$f);};
You can do it if you use the older [:parse] functionality. I wanted a function to do some debug logging of a script, but only when I needed it.
Notice the line where I set logfn. “$ident” refers to the value of the local “ident” variable at the time that line is executed. “$1” refers to the first parameter of the log function when the log function is executed.
Of course, this only allows access to the values of local variables when the function is defined, not the values those variables have when the function is running.
# Declare variables to be used by $log
:local verbose true
:local ident "install"
# Create $log
:local logfn ""
:if (verbose) do={:set logfn ":log info \"$ident: \$1\";"}
:local log [:parse $logfn]
# Use $log
$log "Starting"
Despite this thread’s age, I use functions with positional/named parameters all the time in ROS script. This works great if the function is declared in global scope, but when a function is declared as a member of an array, things get more confusing/difficult.
While functions in a :global array can uses positional/named arguments… The problem is the first parameter is $0, not $1. I wrote up the gory details of how this seemingly works here: Positional Arguments in Array Function - $0 vs $1?. But the problem is if you re-assign a function from one global to array member, it doesn’t work since the “moved” function references $1 as the first positional param while a function in an array uses $0 for the first param.
My feature request is for “functions in arrays” the value of $0 should be array that contains the function, or at least be nil/[:nothing], so the positional arguments don’t change if the function is executed via an array member or directly from a :global.
I say this since if functions stored within an array had access to the value of the containing array (say via $0), ROS functions would look more like a C++ vtable with a “this” pointer (e.g. $0 = array storing the function) thus allowing a limited form of a class.