make a subroutine from a number

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
B Swartzwelder
make a subroutine from a number

Post by B Swartzwelder » Wed Dec 22, 2004 7:15 am

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?

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Thu Dec 23, 2004 8:52 pm

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:

Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

B Swartzwelder

Thanks a million.

Post by B Swartzwelder » Sat Dec 25, 2004 4:11 am

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.

Guest

Post by Guest » Sat Dec 25, 2004 9:13 pm

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.

Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Sat Dec 25, 2004 9:36 pm

Sorry, I had not logged in when I posted last, and I came out as as "Guest" :D
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Post Reply