Vic's QBasic Programming Tutorial Basic Tutorial XI SUB's Didn't think you needed them did you? ----------------------------------------------------------------------------- This tutorial will cover how to use a function in QBasic that is very unused but is very important. With a Sub function you can have very very organized code... First, for the Newbie, what is a SUB? You can consider a SUB a SUB PROGRAM. It can be a program in itself. I have found many QB games on the internet that only use subs to do everything! I personnally think thats insane, but I am sure that they have a good reason. What can a SUB do? Whith a sub you can make your own QBasic Commands! With a sub you can ask qbasic to do a series of commands by just typing one command... ----- Lets start out with an example of this... Pretend that you wanted one command to print out the Date and the TIME at the same time... How would you do this? Here we go! Open QBASIC... Goto Edit click on New Sub Type in "VTime" (without quotations...) Hit Enter Hit enter again Type in PRINT TIME$ press enter Type in PRINT DATE$ Push F2 on the keyboard (or click on the bottom of the screen that says F2 = SUB ) Double click on the original program, (Should be Untitled) Type Vtime Push F5 to run the program... There you go... You made your first SUB!!! If you push F2 and go back to the Vtime sub you can see what two commands are run when you call it... Before I continue try and make your own sub from scratch so it gets implanted in your memory better... ------------------------------- Putting input into a sub... Have you ever downloaded a program where the sub says SUB Commandname (x%, y%, cle%) ? What this does is give the sub that is being called input... (the percent (%) signs are not needed) Remember this little beginner program? name$ = "Vic" print "Hello "; name$ lets put this into a BAS... create a new sub and name it printN When you are in the new sub type this for the top line It allready sould be SUB printn END SUB make it look like this... SUB printn (name$) End sub Between the SUB and END SUB part write this PRINT "Hello ";name$ The sub should now look like this... SUB printn (name$) PRINT "Hello ";name$ END SUB Now, press F2 and go to the original program and type... CALL printn ("yournamehere") Run the program! Look, the program prints out your name! With only one line of code! You can call it over and over and it will change each time... ------------------------------------------------------------------------ More than one Input... Start a new program in QBasic... In this example we will use the LOCATE command and Print to print out a word on a section of the screen you pick... !!!!!!!!!!!!! !SHORTCUT:::! !!!!!!!!!!!!! Instead of going to edit and new sub do this... In the current program just type SUB subname When you push enter a sub will be made for you and you don't have to go through all of those menus! !!!!!! Ok, type SUB VPrint (x, y, Words$) and push enter Now, type these commands... LOCATE x,y : print Words$ Push F2 and go back to the main program... type in... CLS CALL vprint (10, 15, "Hello World!") RUN IT!!! I really hope I went into enough detail for you... Thats it... ----------------------------------------------------------------------------- ***************************************************************************** ----------------------------------------------------------------------------- Thats it for this tutorial, If I didn't get into enough detail in the explanations then just look at the source code and try to figure it out on your own. All else fails E-Mail Me... My current E-Mail address is RADIOHANDS@AOL.com If you are using this tutorial on your page, please leave the tutorial exactly as it is... please don't change anything, unless its spelling errors... Theres alot of them! I don't like using the backspace key... The original website that these were on is http://members.aol.com/radiohands/index.html Thank you Vic Luce Finished December 9 1999 If you want to be notified when a new tutorial is out.. Send An E-mail to RADIOHANDS@AOL.com with the subject saying VQBLIST and then your E-mail address(check website)