Community discussions

MikroTik App
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Functions and function parameters

Wed Jul 24, 2013 4:21 pm

Starting from v6.2 we have added new syntax which allows to define functions and even pass parameters to them

ros code

:global myFunc do={
   :put "arg a=$a";
   :put "arg '1'=$1"
}                          
$myFunc a="this is arga value"  "this is arg1 value"
Output:
arg a=this is arga value
arg '1'=this is arg1 value
Read more in documentation:
http://wiki.mikrotik.com/wiki/Manual:Sc ... #Functions
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Wed Jul 24, 2013 4:56 pm

I just want to say one thing: I LOVE YOU!

This is something that will make my daily work so much easier :)
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Functions and function parameters

Thu Jul 25, 2013 12:35 pm

Nice! 8)

Just one question... if I have a script in script environment, must I ":parse" it into a variable before I'm able to call it with parameters, or does this syntax apply to the run command as well? i.e. could I do something like

ros code

/system script run myScript myVar=myValue
And analogously for "/import" scripts?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Thu Jul 25, 2013 12:52 pm

You can't pass variables with run command. You will need to parse script into variable as shown in the examples.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Functions and function parameters

Thu Jul 25, 2013 2:20 pm

You can't pass variables with run command. You will need to parse script into variable as shown in the examples.
OK, but... Could we hopefully get that in the (near?) future?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Wed Jul 31, 2013 12:24 pm

Few more features:

* Now function can return value with :return command
* Catch run-time errors

ros code

:do {
      :put [:resolve test.com];
} on-error={ :put "resolver failed"};
:put "lala"
output:
resolver failed
lala
http://wiki.mikrotik.com/wiki/Manual:Sc ... ime_errors
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Wed Jul 31, 2013 1:03 pm

Keep it up, thats some great additions.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Functions and function parameters

Wed Jul 31, 2013 3:07 pm

Similarly to the question before... can one :return from a script/import?

(Yeah, I really prefer abstracting away utility functions away, for re-usability's sake)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Wed Jul 31, 2013 3:16 pm

Similarly to the question before... can one :return from a script/import?

(Yeah, I really prefer abstracting away utility functions away, for re-usability's sake)
Currently no, but as a workaround you can add scheduler to run on startup script which will import all required scripts into :global variables.
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Sat Aug 03, 2013 3:15 pm

I got time today to play with this, so a few questions:

1) It seems you cant pass variables to functions, this should work, but doesnt:

ros code

:global PutValue do={
	:put $1
}

:for i from=1 to=3 do={
	:put $i
	$PutValue $i
}
2) Can you not define functions as :local? This should work, but doesnt:

ros code

{
:local PutString do={
	:put function
}

:for i from=1 to=3 do={
	:put $i
	$PutString
}
}
 
Ivoshiee
Member
Member
Posts: 483
Joined: Sat May 06, 2006 4:11 pm

Re: Functions and function parameters

Sun Aug 04, 2013 12:10 am

Finally something to improve the scripting, but do you plan to implement JIT compilation of the scripts or some other means to lower the CPU utilization as well? It is needed because the scripting engine on ROS is killing the system performance - it is too simple to implement a script that is doing nothing, but CPU load is at 100%.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Mon Aug 05, 2013 11:32 am

tomaskir these problems will be fixed in next release.
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Mon Aug 05, 2013 11:33 am

tomaskir these problems will be fixed in next release.
Thanks!
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Tue Aug 06, 2013 5:38 pm

Another thing which doesnt work is functions accessing local variables:

ros code

{
:local sText "This is a sting of text."

:global putString do={
	:put $sText
}

$putString
:put $sText
}
So since I cant at the moment define local functions yet, I have to keep all my variables and all my functions as global, which is a mess and creates potential for naming conflicts :)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Tue Aug 06, 2013 5:51 pm

Of course it will not work, function is function and it can't see local variables defined in main scope.

You need to pass parameter when executing function

for example:

ros code

:global sText "This is a sting of text."
:global putString do={
  :put $txt
}
$putString txt=$sText
Since there is a bug with local vars being passed. I am using global as an example
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Tue Aug 06, 2013 6:22 pm

You mentioned that you will fix functions to work in local scope in next release. So theoretically:

Since the whole script is enclosed in { }, the whole script is a single scope.
:local functions defined in this scope should be able to access other :local variables defined in the same scope.

So will it be possible (in next release when :local functions work) to do this, since the function and variable are in same scope?

ros code

{
# local variable
:local sText "This is a sting of text."

# local function
:local putString do={
	:put $sText
	:set sText "Now a different string of text"
}

$putString
:put $sText
}
As you said, you can acomplish this with passing an argument to the function, but if it also works this way, it gives us more options :)
I dont have to pass an argument and then return a value, all is handled with the variable, so less code to accomplish the same.

-----

Also, one more request, since it seems you guys are actively working in this area right now. Would it be possible to implement a "date" variable type? We have a "time" type which works great. Example:

ros code

{
:local curTime
:local curDate

:set curTime [/sys clock get time]
:put [:typeof $curTime]

# works great with compares etc
:if ($curTime > "12:00:00") do={
	:put "Its after noon"
} else={
	:put "Its before noon"
}

# also works great
:put ($curTime + 1h)

:set curDate [/sys clock get date]
:put [:typeof $curDate]

# doesnt work since type is str
:if ($curDate > "jan/01/2013") do={
	:put "We are after 2013"
} else={
	:put "We are before 2013"
}

# doesnt work since type is str
:put ($curDate + 1d)
}
If a "date" type existed and was handled the same way as a "time" type, it would save me SO MUCH work in scripting. It would only take a couple of hours for a programmer to make this, but would save hundreds :)

Also thanks to listening to my silly requests :)
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Wed Aug 07, 2013 10:34 am

As you said, you can acomplish this with passing an argument to the function, but if it also works this way, it gives us more options :)
I dont have to pass an argument and then return a value, all is handled with the variable, so less code to accomplish the same.
You will never be able to access local variables (defined outside function) inside function. For that use global variables.
 
User avatar
tomaskir
Trainer
Trainer
Posts: 1162
Joined: Sat Sep 24, 2011 2:32 pm
Location: Slovakia

Re: Functions and function parameters

Wed Aug 07, 2013 11:53 am

You will never be able to access local variables (defined outside function) inside function. For that use global variables.
Understood, will either pass argument and return value, or use a :global.

Any info on the "date" type?
 
Feklar
Forum Guru
Forum Guru
Posts: 1724
Joined: Tue Dec 01, 2009 11:46 pm

Re: Functions and function parameters

Tue Sep 10, 2013 10:55 pm

Few more features:

* Now function can return value with :return command
* Catch run-time errors

ros code

:do {
      :put [:resolve test.com];
} on-error={ :put "resolver failed"};
:put "lala"
output:
resolver failed
lala
http://wiki.mikrotik.com/wiki/Manual:Sc ... ime_errors
Do you have an example of using the :return feature? I cannot find an example of it on the Wiki.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Thu Sep 26, 2013 1:45 pm

 
Feklar
Forum Guru
Forum Guru
Posts: 1724
Joined: Tue Dec 01, 2009 11:46 pm

Re: Functions and function parameters

Thu Sep 26, 2013 10:51 pm

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?
 
User avatar
mrz
MikroTik Support
MikroTik Support
Topic Author
Posts: 7038
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Functions and function parameters

Fri Sep 27, 2013 10:42 am

Yes of course you can use returned value anyway you like. for example

ros code

[admin@x86] > :global sum do={ :return ($1 + $2)}
[admin@x86] > :put (14 - [$sum 3 4])  
7
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: Functions and function parameters

Mon Sep 30, 2013 11:10 am

do anything you like with result:
:global lala [$sum 3 4]
now vaiable lala will hold the value of the return.
 
sten
Forum Veteran
Forum Veteran
Posts: 919
Joined: Tue Jun 01, 2004 12:10 pm

Re: Functions and function parameters

Fri Feb 21, 2014 4:03 pm

Finally something to improve the scripting, but do you plan to implement JIT compilation of the scripts or some other means to lower the CPU utilization as well? It is needed because the scripting engine on ROS is killing the system performance - it is too simple to implement a script that is doing nothing, but CPU load is at 100%.
With or without JIT compilation, it will always be simple to write bad code.
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Functions and function parameters

Fri Feb 21, 2014 4:17 pm

Finally something to improve the scripting, but do you plan to implement JIT compilation of the scripts or some other means to lower the CPU utilization as well? It is needed because the scripting engine on ROS is killing the system performance - it is too simple to implement a script that is doing nothing, but CPU load is at 100%.
With or without JIT compilation, it will always be simple to write bad code.
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.
 
User avatar
mishaM
Frequent Visitor
Frequent Visitor
Posts: 84
Joined: Sun Oct 25, 2009 1:48 pm
Location: Georgia

Re: Functions and function parameters

Sat Mar 01, 2014 4:49 pm

Hi dear Mikrotik Team,


I create function , and can't retrieve datas

ros code

[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             110
when i convert this command to function, it not working

ros code

[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
 #      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
[misha@test] >
 
efaden
Forum Guru
Forum Guru
Posts: 1708
Joined: Sat Mar 30, 2013 1:55 am
Location: New York, USA

Re: Functions and function parameters

Sat Mar 01, 2014 5:15 pm

Finally something to improve the scripting, but do you plan to implement JIT compilation of the scripts or some other means to lower the CPU utilization as well? It is needed because the scripting engine on ROS is killing the system performance - it is too simple to implement a script that is doing nothing, but CPU load is at 100%.
With or without JIT compilation, it will always be simple to write bad code.
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.
+1. A true language would be nice.

Sent from my SCH-I545 using Tapatalk
 
User avatar
skot
Long time Member
Long time Member
Posts: 584
Joined: Wed Nov 30, 2011 3:05 am

Re: Functions and function parameters

Sat Mar 01, 2014 8:57 pm

Hi dear Mikrotik Team,


I create function , and can't retrieve datas

ros code

[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             110
when i convert this command to function, it not working

ros code

[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
 #      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
[misha@test] >
If you run this, you can see that the passed parameter type is "str" (string) and needs to be converted to an IP.

ros code

:global route do={:put [:typeof $1]; /ip route print where $1 in dst-address dynamic}
$route 172.16.2.1
You can do it this way when you call the function:

ros code

: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:

ros code

:global route do={/ip route print where [:toip $1] in dst-address dynamic}
$route 172.16.2.1
 
User avatar
mishaM
Frequent Visitor
Frequent Visitor
Posts: 84
Joined: Sun Oct 25, 2009 1:48 pm
Location: Georgia

Re: Functions and function parameters

Sun Mar 02, 2014 8:57 am

Hi dear Mikrotik Team,


I create function , and can't retrieve datas

ros code

[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             110
when i convert this command to function, it not working

ros code

[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
 #      DST-ADDRESS        PREF-SRC        GATEWAY            DISTANCE
[misha@test] >
If you run this, you can see that the passed parameter type is "str" (string) and needs to be converted to an IP.

ros code

:global route do={:put [:typeof $1]; /ip route print where $1 in dst-address dynamic}
$route 172.16.2.1
You can do it this way when you call the function:

ros code

: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:

ros code

:global route do={/ip route print where [:toip $1] in dst-address dynamic}
$route 172.16.2.1

thanks! , problem solved with [:toip $1] :)
 
MarcusH
just joined
Posts: 16
Joined: Thu Aug 02, 2012 11:06 am

Re: Functions and function parameters

Fri Jun 27, 2014 7:53 pm

Is there a way to get an array of the unnamed/positional parameters?

For example, if "args" were the array of positional/unnamed parameters, this would work:
:global myFunc=do {
    :put $args
}

$myFunc "1" "2"
1:1;2:2
This would make functions with a variable number of parameters possible.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Functions and function parameters

Fri Jun 27, 2014 9:37 pm

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.
 
Clauu
Member Candidate
Member Candidate
Posts: 217
Joined: Fri Mar 21, 2014 8:27 pm
Location: RO

Re: Functions and function parameters

Sun Aug 31, 2014 7:01 pm

Is there any function for archiving files/folders? tar for ex
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Functions and function parameters

Mon Sep 01, 2014 12:41 am

no one.
 
User avatar
dissident76
just joined
Posts: 10
Joined: Fri Apr 10, 2015 7:33 am

Re: Functions and function parameters

Mon Apr 13, 2015 5:11 pm

Starting from v6.2 we have added new syntax which allows to define functions and even pass parameters to them

ros code

:global myFunc do={
   :put "arg a=$a";
   :put "arg '1'=$1"
}                          
$myFunc a="this is arga value"  "this is arg1 value"
Output:
arg a=this is arga value
arg '1'=this is arg1 value
Read more in documentation:
http://wiki.mikrotik.com/wiki/Manual:Sc ... #Functions

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);};
 
rftnon
newbie
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

Re: Functions and function parameters

Wed Mar 09, 2016 11:23 pm

Thanks!
useful ... 8)
 
korniza
newbie
Posts: 26
Joined: Fri Jan 06, 2012 4:05 pm

Re: Functions and function parameters

Fri Mar 11, 2016 5:35 am

mrz functions script is a lifesaver if you start coding real big scripts! :D
Thanks mrz for sharing!
 
korniza
newbie
Posts: 26
Joined: Fri Jan 06, 2012 4:05 pm

Re: Functions and function parameters

Fri Mar 11, 2016 6:34 am

how do you call the Functions* after running this script? Do I have to use
 /system script run "Functions"
or there is another fancy way( for ex.
:local testfunc $Functions
)
Starting from v6.2 we have added new syntax which allows to define functions and even pass parameters to them

ros code

:global myFunc do={
   :put "arg a=$a";
   :put "arg '1'=$1"
}                          
$myFunc a="this is arga value"  "this is arg1 value"
Output:
arg a=this is arga value
arg '1'=this is arg1 value
Read more in documentation:
http://wiki.mikrotik.com/wiki/Manual:Sc ... #Functions

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);};
 
icosasupport
newbie
Posts: 29
Joined: Fri Oct 13, 2017 8:37 pm

Re: Functions and function parameters

Wed Nov 22, 2017 7:11 pm

how do you call the Functions* after running this script? Do I have to use
 /system script run "Functions"
or there is another fancy way( for ex.
:local testfunc $Functions
)
I have some examples of that here viewtopic.php?f=9&t=40507#p627016

Icosa.
 
DaleNicholsSTG
just joined
Posts: 3
Joined: Fri Aug 03, 2018 1:47 am

Re: Functions and function parameters

Fri Aug 03, 2018 2:02 am

You will never be able to access local variables (defined outside function) inside function. For that use global variables.
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"
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Functions and function parameters

Sun Jan 30, 2022 9:55 pm

Is there a way to get an array of the unnamed/positional parameters?
This would make functions with a variable number of parameters possible.
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.
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.
 
odge
Member Candidate
Member Candidate
Posts: 110
Joined: Mon Nov 29, 2010 2:53 pm

Re: Functions and function parameters

Wed May 18, 2022 7:51 pm

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.
been using for ages... never thought about that idea for checking number of things passed. useful for backward compatability. thanks
 
odge
Member Candidate
Member Candidate
Posts: 110
Joined: Mon Nov 29, 2010 2:53 pm

Re: Functions and function parameters

Wed May 18, 2022 8:13 pm

Starting from v6.2 we have added new syntax which allows to define functions and even pass parameters to them

ros code

:global myFunc do={
   :put "arg a=$a";
   :put "arg '1'=$1"
}                          
$myFunc a="this is arga value"  "this is arg1 value"
Output:
arg a=this is arga value
arg '1'=this is arg1 value
Read more in documentation:
http://wiki.mikrotik.com/wiki/Manual:Sc ... #Functions
Do you think you could add perhaps pass parameters by reference (instead of building it that you return (limited to one variable). I know the answer may be "but then declare it as a global variable to access it". That does limit code reusability a lot. (One function to work on many things).
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Functions and function parameters

Thu May 19, 2022 1:51 am

I didn't understand, can you explain yourself better with an example?
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Functions and function parameters

Thu May 19, 2022 1:39 pm

contribution removed
Last edited by msatter on Sat Jun 11, 2022 1:51 pm, edited 1 time in total.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3169
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Functions and function parameters

Wed Jun 01, 2022 11:32 pm

I didn't understand, can you explain yourself better with an example?
I like @msatter's "just return an array" approach. But if the count of unnamed args is what's needed, this would work for most types (e.g. NOT key-values NOR array-of-arrays):

:global "how-many-args-passed" do={
    :local argv [:toarray "$1,$2,$3,$4,$5,$6,$7,$8"]
    :put "I got $[:len $argv] unnamed arguments"
}

$"how-many-args-passed" one
	# I got 1 unnamed arguments
$"how-many-args-passed" one two
	# I got 2 unnamed arguments
$"how-many-args-passed" one two 3
	# I got 3 unnamed arguments
$"how-many-args-passed" one two 3 four
	# I got 4 unnamed arguments

Who is online

Users browsing this forum: No registered users and 11 guests