Converts a length held in three variables, Y yards, F feet and I inches, into normal form, so that inches run 0–11 and feet 0–2. It totals everything in inches, then divides back out into feet and yards. Negative lengths are handled by working with the absolute value and restoring the sign at the end. The routine starts at line 1000 and ends with RETURN, ready to be called with GOSUB 1000 from another program.
This is the worked subroutine example from the subroutines chapter of the Sinclair ZX81 / Timex Sinclair 1000 BASIC manual. It came from an unlabeled tape, which holds two identical recordings, one saved as PRT 1 FT TO INCHES.
Program Analysis
Program Structure
Line 900 is a REM with the program name. Line 1010 converts to inches with 36*Y+12*F+I. Line 1030 saves the sign in E using SGN, and 1040 takes ABS. Lines 1060–1090 split off feet with INT (I/12) and yards with INT (F/3), multiplying each part by E so all three results carry the original sign. There is no calling program on the tape, so the routine does nothing if run by itself until it reaches RETURN, which stops with an error because no GOSUB is active.
Source Code
900 REM "PRT 1 FT TO INCHES"
1000 REM SUBROUT TO ADJUST Y,F,ITO THE NORMAL FORM FOR YARDS,FEET AND INCHES
1010 LET I=36*Y+12*F+I
1020 REM NOW EVERYTHING IS IN INCHES
1030 LET E=SGN I
1040 LET I=ABS I
1050 REM WE WORK WITH I POSITIVE,HOLDING ITS SIGN IN E
1060 LET F=INT (I/12)
1070 LET I=(I-12*F)*E
1080 LET Y=INT (F/3)*E
1090 LET F=F*E-3*Y
1100 RETURNNote: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.