CoCoA:Return

From ApCoCoAWiki

Return

exit from a structured command

Description

This function is used to exit from a structured command. The latter

form returns the value of the expression E to the user. If executed

within a nested loop or inside a user-defined function, it breaks out back to the top level, not just to the next higher loop. (For breaks to the next higher loop, see <ttref>Break</ttref>.)

Example

  Define Rev(L) -- reverse a list
    If Len(L) &lt; 2 Then Return L EndIf;
    M := Rev(Tail(L)); -- recursive function call
    H := Head(L);
    Return Concat(M,[H]);
  EndDefine;
  Rev([1,2,3,4];
[4, 3, 2, 1]
-------------------------------
  -- Another example: returning from a nested loop
  For I := 1 To 5 Do
    For J := 1 To 5 Do
      If J &gt; 2 Then Return Else Print [I,J], <quotes> </quotes>; EndIf
    EndFor;
  EndFor;
[1, 1] [1, 2]
-------------------------------

Syntax

Return
Return E

where E is an expression.

Break

Define

   <type>function</type>
   <type>loops</type>