﻿ Internal and External Procedures

Internal Procedures and External Procedures

Internal function definitions including DEF-line, internal subprogram definitions, internal picture definitions and detached handlers(HADLER～END HADLER) are called internal procedures.
Besides, external function definitions, external subprograms and external picture definitions are called external procedures.
Furthermore, The main program and external procedures are called program units.

Internal procedures are written in a program unit, and share variables and the declaration such as option base or option angle with the program unit.
A Program unit is an individual object and does not share variables except parameters and the declarations with any other program unit.

Example.1 Internal Procedure

```100  FUNCTION fact(n)
110    LET p=1
120    FOR i=1 TO n
130        LET p=i*p
140    NEXT i
150    LET fact=p
160  END FUNCTION
170  FOR i=1 TO 10
180     PRINT fact(i)
190  NEXT i
200  END  ```

The internal function defined in lines 100 to 160 calculates the factorial of n.
But this program does not act as expected because when the FOR-loop in lines 120 to 140 is executed, the value of i, which is used on the FOR-loop in lines 170 to 190, is changed.

Example 2. External procedure

```160 DECLARE EXTERNAL FUNCTION fact
170 FOR i=1 TO 10
180    PRINT fact(i)
190 NEXT i
200 END
300 EXTERNAL FUNCTION fact(n)
310 LET p=1
320 FOR i=1 TO n
330    LET p=i*p
340 NEXT i
350 LET fact=p
360 END FUNCTION```

On the above program, as the variable i in lines 170 to 190 and the variable i in line 320 to 340 are different variables, the execution result shall be as expected.