• Aucun résultat trouvé

more RETURN statements in a program than GOSUB state- LJ

Dans le document Publishing is (Page 192-195)

ments. Just make sure that for every GOSUB executed, exactly

one RETURN is executed. The example program can be sim- y ] plified a little bit by replacing every GOTO 200 with a LJ RETURN. Line 200 is no longer needed.

160 IF C$="RED" THEN POKE A,2 : RETURN :rem 0 j j

170 IF C$="YELLOW" THEN POKE A,7 : RETURN :rem 7 t—J 180 IF C$="GREEN" THEN POKE A,5 : RETURN :rem 155

200 :rem 146

Subroutines are a second form of repetition, with loops being the first. Like a loop, a subroutine is a sequence of statements which occurs once in a program, but will have been executed many times by the time the program is done. The difference is that every time a subroutine is entered, the sequence of state ments is executed only once. The placement of GOSUB state ments within a program is what controls how many times the statements in the sequence are repeated.

Summary

• A subroutine is a sequence of statements that occurs once in a program, but is executed from several places in the

program.

• The purpose of using a subroutine is to avoid duplicating statement sequences in a program.

• The statements used to implement subroutines are GOSUB (go to subroutine) and RETURN (return to main program).

• The syntax for the GOSUB statement is the keyword

GOSUB followed by a line number. The RETURN statement consists only of the keyword RETURN.

• The GOSUB statement makes execution jump to the begin ning of the specified line. If the specified line does not exist,

an UNDEFD STATEMENT error will occur. [J

• The main difference between GOSUB and GOTO is that

with GOSUB, the computer remembers where execution .

stopped in the main program. LJ

• Because execution eventually resumes where it left off in the

main program, statements can be placed after a GOSUB on (—

the same line. L_

• The RETURN statement causes execution to jump to the first

statement after the most recent GOSUB. This may mean r I

182

u

n

n

Subroutines

n

jumping into the middle of a line. RETURN is comparable to the END statement of a main program.

R • RETURN should be used only when a GOSUB has been

! previously executed. If the computer encounters a RETURN

without having previously executed a matching GOSUB, the P| place to jump back to is undefined, so a RETURN WITH-1 OUT GOSUB error is printed.

• A subroutine can contain several RETURN statements, pro vided that for every GOSUB executed, exactly one RETURN is executed.

• Subroutines are usually put after the main program. An END statement is needed to separate the main program from the subroutine. Having a main program accidentally fall into a subroutine is a sure way of getting the RETURN

WITHOUT GOSUB error.

• When execution is transferred to a subroutine, it is said that the subroutine has been called.

• To make a subroutine more flexible, it can use a variable that is assigned just before the subroutine is called.

• The entry point of a subroutine is the line to which execution jumps when the subroutine is called. Subroutines can have more than one entry point.

Multiple Subroutines

A program can have more than one subroutine.

100 MV=54296:AD=54277:SR=54278:FL=54272:FH=54273:C

T=54276 :rem 211

110 POKE FL,0 : POKE MV,15 :rem 77 120 PRINT "FREQUENCY (HIGH)11; : L=255 : GOSUB 210

{SPACE}: POKE FH,N : N=0 :rem 212

130 PRrNT "ATTACK RATE"; : GOSUB 200 : E=N:rem 218 140 PRINT "DECAY RATE"; : GOSUB 200 : POKE AD,16*E

f"* +N :rem 232

150 PRINT "SUSTAIN LEVEL"; : GOSUB 200 : E=N

:rem 151

— 160 PRINT "RELEASE RATE"; : GOSUB 200: POKE SR,16*

I [ E+N :rem 165

170 POKE CT,129 : GOSUB 230 : PRINT "*" :rem 170 180 POKE CT,128 : GOSUB 230 : PRINT :rem 60

f—| 190 GOTO 120 :rem 102

' ! 200 L=15 :rem 129

210 INPUT N : IF N<0 OR N>L OR N<>INT(N) THEN PRIN T "OUT OF RANGE" : GOTO 210 :rem 96

| j 220 RETURN : rem 116

Subroutines When you run this program, you must specify a frequency value from 0 to 255, and envelope values from 0 to 15. The program will start the sound, using the noise waveform. An asterisk is printed when the sound is released. After that, the program repeats for a different sound.

This program uses two subroutines. The one starting at line 230 is a standard delay loop. The other subroutine is used to input and check the range of a number. It has two entry points: line 200 for when the range is from 0 to 15, and line 210 for when the range is from 0 to some other number.

Because the two subroutines are called separately and occupy different lines in the program, they coexist without any prob lems. You should not get the RETURN WITHOUT GOSUB

error.

The example shows that a program can have several sub routines, each one independent of the others. How about hav ing two or more subroutines in effect at the same time? Can one subroutine call another subroutine? The answer is yes, provided that for every GOSUB executed, exactly one RETURN is executed. When one subroutine is called from within another, the subroutines are said to be nested.

10 REM DEMO OF NESTED SUBROUTINES 20 PRINT "MAY

A well-known saying will be printed when you run this pro gram. There are two subroutines in this example. The first subroutine starts at line 50, and it calls the other one which starts at line 80. Trace through the program to see the order in which the statements are executed. First there is the GOSUB in line 30, then the GOSUB of line 60, followed by the RETURN on line 90, and the RETURN at line 70. Notice that 184

Subroutines

n

! I at no time have more RETURN statements been executed than GOSUB statements. If the order of execution were GOSUB,

^ RETURN, and RETURN, the second RETURN would generate j i the RETURN WITHOUT GOSUB error, because no

corresponding GOSUB had previously been executed. Also,

^^ once the program is finished, there have been just as many

! { RETURN statements executed as GOSUB statements.

Of course, the example is not a practical application of nested subroutines. It is intended only to show you how nest ing works.

Nested subroutines can often be found in larger programs.

Commodore 64 BASIC has a maximum nesting limit of 23 lev els, and in certain circumstances this limit is even less. If a program ever exceeds the nesting capacity of BASIC, the error message OUT OF MEMORY will be printed, even if there are plenty of free bytes still available. With simple programs, this should never happen.

There is one last possibility to be considered. What if a subroutine calls itself? This technique, called recursion, is the third form of repetition, after loops and subroutines. Many interesting things can be done using recursion, but it is diffi cult to program properly, and few of our present programming needs require that we use it. Recursion is an advanced topic that is beyond the scope of this book. For now, just be careful to avoid program lines like the next one, which is not only pointless, but is also a sure way of producing the OUT OF MEMORY error.

100 GOSUB 100 :rem 162

Summary

• Several subroutines can be used in one program.

• When one subroutine calls another subroutine, the

Dans le document Publishing is (Page 192-195)