# EXIT Terminate Subprogram and Return

> Source: https://manual.pvxplus.com/directives/exit

## Formats

|  |  |  |
| --- | --- | --- |
| 1\. | *[Terminate Subprogram](https://manual.pvxplus.com/directives/exit#Mark6):* | **EXIT** \[**ERR** *\| err_val* \] |
| 2\. | *[Terminate Procedure, Specify Value](https://manual.pvxplus.com/directives/exit#Mark7):* | **EXIT WITH** *expression*\[$\] |

***Where:***

|  |  |
| --- | --- |
| *err_val* | Numeric expression whose value will be returned as an error status to the program initiating the subprogram (e.g. via a **CALL** directive). |
| *expression\[$\]* | Value to be returned from a multi-line function, embedded I/O procedure, or OOP method logic. String or numeric expression. |

Important Note:

*You can only use this directive in subprograms*; otherwise, PxPlus returns Error #37: Directive can only execute in subprogram.

## Description

Use the **EXIT** directive in a subprogram to terminate the subprogram and return control to the initiating program.

Use **EXIT WITH** to pass a value back from a multi-line function, an embedded I/O procedure, or OOP method logic.

*(EXIT WITH was added in PxPlus 2018.)*

## Format 1

***Terminate Subprogram***

**EXIT** \[**ERR** *| err_val* \]

You can have the subprogram return an error code value to the calling program by using **EXIT ERR** or by specifying the error value following the **EXIT** directive. Use an integer from 0 to 32767 for the error value. To have the calling program process an error value other than 0, use the **ERR=** option in the **CALL** directive (or use **SETERR** *numeric expression*). A value of 0 which generally indicates record busy and force an automatic retry will not when generated by an **EXIT** directive.

When you use **EXIT** in a compound statement, it must be the ***final*** directive:

```
9000 print "Subroutine done"; exit
```

See **[Example](https://manual.pvxplus.com/directives/exit#Mark5)**.

## Format 2

***Terminate Procedure, Specify Value***

**EXIT WITH** *expression*\[$\]

The **EXIT WITH** directive is similar to the **RETURN** *expression*\[$\] directive; however, it does not require the **GOSUB/FOR** stack to be pre-cleared. **EXIT WITH** serves a similar purpose in a multi-line function, embedded I/O, or OOP function procedure as it does in a typical subroutine; however, it also passes back a value, *expression*\[$\]. See **[Example](https://manual.pvxplus.com/directives/exit#Mark5)** below.

***Multi-Line Function***

When used in a multi-line definition of a user-defined function (**DEF FN**), the **EXIT WITH** directive terminates the function procedure and passes back a result. The specified value must match the data type of the function definition itself.

***Embedded I/O Procedure***

The **EXIT WITH** directive terminates an embedded I/O procedure and returns a value to the file handler, which it may use to determine the next operation.

***OOP Method Logic***

In **[Object Oriented Programming](https://manual.pvxplus.com/pxplus-user-guide/object-oriented-pxplus/introduction)** (OOP), the **EXIT WITH** directive terminates the method logic and passes back a result.

## See Also

[CALL Transfer to Subprogram](https://manual.pvxplus.com/directives/call)  
[PERFORM Call Subprogram, Pass Variables](https://manual.pvxplus.com/directives/perform)  
**[RETURN Return From a Subroutine](https://manual.pvxplus.com/directives/return)**  
**[DEF FN Define Function](https://manual.pvxplus.com/directives/def-fn)**

## Example

Example using **EXIT**:

```
! In subprogram "SUBPR"
TEST_EXIT:
     seterr UNKNOWN_ERROR
     enter TEST$
     T=num(TEST$(5,6))
     exit
UNKNOWN_ERROR:
     exit err
```

Example using **EXIT WITH**:

```
     def class "a"
     function test()Do_Test
     end def
Do_Test:
     for i=1 to 3
          for j=1 to 3
               print "I=",i," J=",j
               if j=2 \
                    then exit with 123
          next
     next
     return 456
```
