3 - 31 3 - 31
MELSEC-Q
3 LET'S CREATE AND EXECUTE A PROGRAM
3.9 Using Subroutines
There are cases when a certain process is repeatedly performed within a program. For
example, if making a bar graph using the character " ,"
RUN
OK
10 READ A
20 FOR I=1 TO A
30 PRINT " " ;
40 NEXT I
50 PRINT
60 READ A
70 FOR I=1 TO A
80 PRINT " " ;
90 NEXT I
100 PRINT
110 DATA 5,9
120 END
The areas marked with are identically the same. If more graphs are to be created, a
long program may be required.
Subroutines are used to call the same process from various locations, treating them as
one "group." Namely, the GOSUB instruction and RETURN instruction are used.
Modify the program above so that it looks like as shown below.
RUN
OK
10 READ A
20 GOSUB 70
30 READ A
40 GOSUB 70
110 RETURN
50 DATA 5,9
60 END
70 FOR I=1 TO A
80 PRINT " " ;
90 NEXT I
100 PRINT
The execution result is the same, but the area marked with is now only one.
'GOSUB 70' in lines 20 and 40 calls the area that starts at line 70 ( Subroutine).
After the area ( Subroutine) is completed, the execution returns to where it left off
by the RETURN instruction in line 110.
The subroutine is used
by two instructions.
END
RUN
Subroutine
GOSUB 70
GOSUB 70
RETURN
RETURN
REMARK
Always use the GOSUB instruction to call subroutines. If the GOTO instruction is
used, an error will be generated at the RETURN instruction.
Содержание A1SD51S
Страница 183: ...11 13 11 13 MELSEC Q 11 INSTRUCTIONS AND FUNCTIONS RUN Before swap A 0 H924 A 1 H1159 After swap A 0 H1159 A 1 H924 OK...
Страница 331: ...11 161 11 161 MELSEC Q 11 INSTRUCTIONS AND FUNCTIONS REMARK See the CON ON OFF STOP and ZOPEN functions and Section 7 4...
Страница 557: ...11 387 11 387 MELSEC Q 11 INSTRUCTIONS AND FUNCTIONS REMARK See the CHR SPACE and SPC functions...
Страница 629: ...11 459 11 459 MELSEC Q 11 INSTRUCTIONS AND FUNCTIONS REMARK See the ZOPEN ZSEND and ZCNTL instructions and Section 7 3 4...
Страница 645: ...11 475 11 475 MELSEC Q 11 INSTRUCTIONS AND FUNCTIONS REMARK See the ZEVENT ZSIGNAL and DEF ZEVENT instructions...