CALL subprograms

From Ninerpedia
Revision as of 15:52, 17 July 2015 by Stefan Haubenthal (talk | contribs) ("code" removed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The TI99/4a used SUB PROGRAMS, used with the word CALL, such as CALL SOUND, CALL COLOR, CALL HCHAR - these are samples of sub programs built into TI BASIC.

The various Extended BASICs added additional CALLs and sometimes modified the TI CALLs a little. Other modules (for example Personal Record Keeping) added CALLs to TI BASIC programs when inserted.

In addition to the many supplied CALLs you can define your own CALLS in Extended BASIC.

Subroutine or Subprogram?

If in a program the colours of character sets 1 to 8 are regularly changed.

It could be done with a subroutine, setting variables and then using GO SUB:

200 X=2 
210 Y=4
220 GOSUB 8OO
230 GOTO 230 
800 FOR T=1 TO 8
810 CALL COLOR(T,X,Y)
820 NEXT T
830 RETURN

In Extended Basic we can create a sub program called COLOUR (NOTE: English spelling! now you can call color or colour, one is built in, one you have written... ) .

Sub programs MUST appear at the end of your program. Only other sub programs can follow them.

Example:

200 CALL COLOUR(2,4)
210 GOTO 210
2000 SUB COLOUR(X,Y) :: FOR T=1 TO 8 :: CALL COLOR(X,Y) :: NEXT T :: SUBEND

Thats it.

In this example of a sub program, we have passed two VALUES to the sub program, which are taken by the local variables X and Y in the subprogram.

IMPORTANT: If X and Y are used in your main program, or indeed in other sub programs, their values are NOT changed by their use here. This X and Y on line 2000 are unique to this sub program.

If instead of numbers. we use variables CALL COLOUR(A,B), then in this form, the values of A and B may be changed in the sub program.

Further example:

 10 CALL EXAMPLE(A,B)
 11 PRINT A,B
 12 GOTO 12
 20 SUB EXAMPLE(X.Y) :: X=2*X :: PRINT X :: SUBEND

In this format, if A=2 and B=4 when the CALL is executed, the values of A and B are passed to X and Y in the sub program. At the end of the sub program, the values of X and Y are passed BACK to A and B, thus the sub program has changed variables in the main program.

If you do not want this to happen, it doesn't have to. You can pass variables with their values protected by using extra brackets:

CALL EXAMPLE((A))(B)) 

The same SUB will take its values from A and B, but at the end of the sub program, A and B will retain their original values: they will not be changed bv the sub program. Just place the variables in the CALL in brackets.


Example User written Sub Programs

The ability to write a program in such a way that you CALL up your own sub-programs, such as:

CALL SETUP 
CALL INSTRUCT 
CALL PLAY 
CALL HIGHSCORES 

CALL's are used all the time even in TI Basic, such as CALL CLEAR, CALL HCHAR, and so on. In Extended Basic you can write your own.

For a very large selection you can do no worse than refer to Jim Petersons NUTS AND BOLTS disks, but in the meantime here is a selection of routines for you from Peter Hutchison. nks Peter.

If you have a disk system, save each of these on disk in MERGE format and then just merge them into any program you wish to use them in.


CALL PUTAT

This command uses the whole screen (32 x 24) rather than the limited 28x24 used by DISPLAY AT. Form: CALL PUTAT(row, column, string) Sample use: CALL PUTAT(5,1,"HELLO TI USERS EVERYWHERE")

27000 SUB PUTAT (R,C,T$) 
27010 IF R<1 OR C<1 OR T$="" THEN SUBEXIT
27020 R=MIN(R,24) :: P=1 
27030 IF R>=24 AND C>32 THEN PRINT :: R=24 :: C=1 
27040 IF C>32 THEN R=R+1 :: C=1
27050 CALL HCHAR(R,C,ASC(SEG$(T$,P,1))) 
27060 IF P>=LEN(T$) THEN SUBEXIT 
27070 C=C+1 :: P=P+l :: GOTO 27030 
27080 SUBEND 


CALL SAVECHAR / CALL LOADCHAR

These commands will save the definitions of the characters 96 to 127 into an array PAT$ and then use the array to restore the definitions. Examples: CALL SAVECHAR(PAT$()) and CALL LOADCHAR(PAT$())

27220 SUB SAVECHAR(PAT$()) 
27230 FOR C=96 TO 124 STEP 4 :: EL=C/4-23 :: CALL CHARPAT(C,P$(EL)) :: NEXT C 
27240 SUBEND 
27250 SUB LOADCHAR(PAT$()) 
27260 FOR C=96 TO 124 STEP 4 :: EL=C/4-23 :: CALL CHAR(C,P$(EL)) :: NEXT C 
27270 SUBEND 


CALL HSCROLL

This command can he used to scroll text along a line on the screen Form: CALL HSCROLL(row, column, length_disp1ayed, text$) Sample: CALL HSCROLL(5,9,10,TEXT$) Note: Text is limited by the system and the code below to a length of 240 chars!

27300 SUB HSCROLL(R,C,L,H$) 
27310 M$="-------"&H$&"-------" 
27320 IF LEN(M$)<L THEN 27310 
27350 FOR A=1 TO LEN(M$)-L+1 
27340 DISPLAY AT(R,C)SIZE(L):SEG$(M$,A,L) 
27350 FOR B=1 TO 15 :: NEXT B :: NEXT A 
27360 SUBEND 


CALL GET

This command will return the CHARACTER of the key pressed, or a nul if no Key is pressed. Form: CALL SET (KEY$)

27380 SUB GET (K$) 
27390 CALL KEY(O,K,S) 
27400 IF S=0 THEN K$="" ELSE K$=CHR$(K) 
27410 SUBEND