Page 1 of 1
make a subroutine from a number
Posted: Wed Dec 22, 2004 7:15 am
by B Swartzwelder
I have a program which reads some data (numbers from 0 to 31) from a file part of the time and for the rest of the time, it creates data in the form of random numbers (also 0 to 31). Once the data is there, it sorts through it by using a number of subroutines. The subroutines are named sbrtne0 through sbrtne31. When a numbe 1 comes up, I use a gosub sbrtne0. If a 17 comes up, I use a sbrtne17 etc., etc. Is there some way to generate the name for the gosub directly from the numbers? Right now, I'm using a select case with 32 lines of cases. I'd like to do something like gosub sbrtne(28) when a 28 comes up. Is it possible?
Posted: Thu Dec 23, 2004 8:52 pm
by Ralph
Here is a method of reducing all those lines of code. I put the basics in the following little program. If you have any questions, let me know.
Code: Select all
CLS
B:
INPUT "Enter a number from 0 to 2, or 100 to end program ";N
IF N >= 0 AND N <= 2 THEN
ON N + 1 GOTO 0, 1, 2
ELSEIF N = 100 THEN
END
ELSE
PRINT N; "is not a valid number. Please try again"
GOTO B
END IF
'subroutines:
0 :
PRINT "0"
GOTO B:
1 :
PRINT "1"
GOTO B:
2 :
PRINT "2"
GOTO B:
Thanks a million.
Posted: Sat Dec 25, 2004 4:11 am
by B Swartzwelder
I never knew a subroutine name could be just a number. You made this look so simple. I'm actually running two outputs each with eight bits and the subroutines are only flashing data on the screen so I can run one set from 0 to 255 and another set from 256 to 511. This is really great and you have my heartfelt thank you.
Bert S.
Posted: Sat Dec 25, 2004 9:13 pm
by Guest
Well, I titled the last part of my program with the title 'Subroutines. Actually, these are not the so-called SUBs that we normally refer to, which always begin with the word SUB and the ending words END SUB. The three "Subroutines" are just sections of code that I GOTO by means of their labels, namely, the 0 :, 1 : and 2 :, and that I get back to the main part of the program with the GOTO B.
I just thought that this was easier than the true SUBs, for which I could not get a number to work. The GOTO, however, goes to any label, including line numbers, so that worked out good.
Posted: Sat Dec 25, 2004 9:36 pm
by Ralph
Sorry, I had not logged in when I posted last, and I came out as as "Guest"