Use RETURN in FOR/NEXT loop

Hello,

I would like to know if it is possible to use the RETURN statement to exit from a FOR/NEXT loop that is contained in a function. In the FOR/NEXT documentation, it is mentioned not to use GOTO to exit a FOR/NEXT loop. Are there any restrictions for RETURN with FOR/NEXT ?

Ex.:
FUNCTION TestLoop ()
FOR $ i% = 1 TB 3 STEP 1
Print $ i%
IF $ i% = 2 THEN
RETURN
ENDIF
NEXT $ i%
ENDFN

Thank you
Christian

Hello,

Let me run some tests and i will let you know what i find.

Hello, If you look at the Basic Programming Reference Guide. In section 2.3.3 it states the following:

Keyword “return” inside Functions
The keyword return can be used at any place inside a function to end it.
Example 3: Use of the Return Keyword
FUNCTION my_function
IF (global_var%=1) THEN
$my_function = 1.0
RETURN
ENDIF
$my_function = 0.0
ENDFN
The current value of the RETURN ($FunctionName) will be returned just as if the ENDFN was
reached.

I have attached the link to the guide below:

Hi Kevin,

It’s strange, everytime there is a RETURN executed that is inside a FOR/NEXT loop to exit the FUNCTION (like in my example), the value of ProgAvailMem "Script code free memory " (in System Counters) decreases. If the RETURN is outside the FOR/NEXT loop the value of ProgAvailMem does not change.

I have the impression that there is a memory link caused by using the RETURN in a FOR/NEXT loop.

Christian

Another information:
If my FUNCTION TestLoop () is called via the “Cyclic Section” the ProgAvailMem does not change, but if it is via an ONTIMER the ProgAvailMem decreases with each call.

Flexy 205 with Firmware: 14.2S0

Thank you
Christian

After the “ProgAvailMem” went to zero, I got the following error:
[ERROR] Out of memory (14) 495: FOR $ i% = 1 To 3 STEP 1

Then “ProgAvailMem” is returned to the starting value and it all starts again.

Christian

Hello,

I am just going through my topics. Are you experiencing abnormal behaviour?

Do you need additional assistance?

Hi Kevin,

I changed the FOR loop to program it with GOTOs and I no longer have the error.

Christian