Community discussions

MikroTik App
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

:return not as described?

Tue Nov 10, 2020 11:51 am

I have the problem that :return acts like an :error and terminates the function and does not execute the last command till the end of the function "}"

From the Wiki:
Starting from v6.2 new syntax is added to easier define such functions and even pass parameters. It is also possible to return function value with :return command

Talked about is that it should return the value but not that the function is terminated on the spot. I read it as, that the value is returned on exit of the function.
Now I have first transfer the :global values to a temporary :local and erase the :global ones and :return the locals. The :local ones are destroyed on exit of the function.

I could switch back complete local but during debugging the environment box shows the values of variables when the are set temporary to global.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: :return not as described?

Tue Nov 10, 2020 1:00 pm

 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: :return not as described?

Tue Nov 10, 2020 2:39 pm

Thanks SiB and I did not see any other that :returns cuts away from the function despite there could be more instructions to process. Or did I missed it?

It has it's chams because you can go in the middle exit a function this way without being concerned about code underneath. It brings the feeling back of a GoTo jump to me.
A beter naming for return would then be exit because return can have here two meanings, values returned or return now the values.

I avoid, if possible, global variables and prefer the self erasing of local, it not being able to life outside the scope. I expected return would help them to life outside the scope but it has it's price this way.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: :return not as described?

Tue Nov 10, 2020 4:02 pm

in any terminal you can type "?" and see two descriptions:
return -- return value from function
quit -- Quit console

And in scripts you can use :quit; command if you interested in stop of parsing scripts.
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: :return not as described?

Tue Nov 10, 2020 5:07 pm

Returning the value and leaving the function is the behavior for every language.

Everything else does not make sense: What would you return if the function continues and a second return is executed. How to leave the function early?
A beter naming for return would then be exit
No, return is to return from a function, exit is to terminate the whole script or program.
There's no exit in RouterOS scripting, but please do not invent it for something uncommon.
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: :return not as described?

Tue Nov 10, 2020 7:37 pm

If there is a second return the that one should supersedes the earlier one and the last one the only one retuned. It is then behaving as variable, which can have only one value.

We have already :error which stops all scripts/and functions.
 
User avatar
SiB
Forum Guru
Forum Guru
Posts: 1888
Joined: Sun Jan 06, 2013 11:19 pm
Location: Poland

Re: :return not as described?

Tue Nov 10, 2020 9:44 pm

msatter
We have already :error which stops all scripts/and functions.
Now I understand, you collect them as like new Pikachu...

Then try next one:
  • removing newest job
    /system script job remove ([/system/script/job/print count-only]-1)
  • massive exit :)
    system reboot
I hope I help :p
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: :return not as described?

Wed Nov 11, 2020 2:35 am

It seems I had them because I had to scare away people looking at their phone wandering into the garden. Never seen them personally so it was a complete surprise to me that I had them in my garden. Those invisible creatures.

Not much running scripts here and sometimes the VPN maintainers flashes by. Still debugging in terminal so maybe when it is ready. ;-)

Who is online

Users browsing this forum: stanisljevic and 23 guests