![]() |
Menu |
QBasic Tutorial #6 When dealing with small programs, it is quite easy to see and understand it's structure. However, as programs get bigger, it becomes more difficult to update so a more modular approach is required. This is where subroutines come into their own. They enable the programmer to break up a program into more manageable chunks. This also have the advantage that they can be called anywhere in the program and indeed more than once. Here is an example of a routine that can be called when the program is first loaded and at other times, usually in response to a menu selection (the menu's have been omitted for the sake of clarity). So as usual, the program looks like this and must be cut and pasted into your template file. DECLARE SUB GetFilename ()
' main program starts here
DIM SHARED Filename$
DIM msg$
Filename$ = "testfile.txt"
GetFilename
msg$ = Filename$ + " entered"
PRINT msg$
' main program ends here
END
' subs start here
SUB GetFilename
DIM TheInput$
INPUT "Enter a Filename: ", TheInput$
IF TheInput$ <> "" THEN
Filename$ = TheInput$
END IF
END SUB
It is imporant to realise that up until now, your programs could be written without any real structure. Now it becomes important to split your programs into the main part and all the subroutines below it. I have shown this structure using comments which can be omitted but for the moment it might be best to keep them in until you get used to this new way of doing things. When you paste this into QBasic you may find that the subroutine has disappeared. Fear not, it hasn't. Press F2 and select GetFilename. To get back to the main part of the program, press F2 and select the name of the program you have saved it as. It could be argued that the comments are not required because of the way QBasic shows the subroutines. However, when it is printed or the program viewed in Notepad, the comments suddenly become important. I'll go through the program a line at a time. DECLARE SUB GetFilename () This is telling QBasic that there is a subroutine of that name. It usually put in automatically when you create a new subroutine. ' main program
starts here The comment reminds us this is the start of the main part of the program. The variable Filename$ is defined here and it is important to realise that this is a shared or global variable. This kind of variable cab be used both by the main program and ALL of the subroutines defined in the program. This is important. It will hold a value that can be changed by any routine in the program and it will hold that value until either the program is restarted or until something changes it. This is useful for filenames as we want to be able to access it from anywhere in the program. Filename$ = "testfile.txt" The global variable is now given a filename. This will write the file to the same location as the QBasic.exe file. GetFilename (Filename$) The subroutine is then called but this is not the end of the main program yet - we will come back to it. When the subroutine is called, execution jumps down to the next line of code. ' sub starts
here After the comment, the
name must be preceded by the keyword SUB
(to denote a SUBroutine) and suffixed by
brackets as shown. INPUT "Enter a Filename: ", TheInput$
IF TheInput$ <> "" THEN
Filename$ = TheInput$
END IF
Note how it is indented to show that we are inside a subroutine and that the IF statements are indented again just to make it absolutely clear what the relationships are and makes it easier to read a listing. This has to be done manually as QBasic won't do it for you. The INPUT keyword is used to get the new filename. If no filename is entered then the local variable is assigned the contents of the global variable above. If a filename is entered then the global variable is assigned the contents of the TheInput$ variable. If the RETURN key is pressed without anything being entered (the <> means NOT) then the original contents is retained. END SUB The subroutine now ends and execution is passed back to the main program that called it. msg$ = Filename$
+ " entered" The local variable msg$ is assigned the filename and displays it using PRINT. The main program then ends and the familiar Press a key" message appears.. You can try running this, first without entering anything at the prompt and then entering a name and see what happens. See also what happens when the program is restarted. One slight problem with the above program is that there are no checks to see if what is entered is a valid filename. The slighly modified program below rectifies this. Again, test it and see what it does. SUB GetFilename
DIM TheInput$
DIM i
INPUT "Enter a Filename: ", TheInput$
IF TheInput$ <> "" THEN
Filename$ = TheInput$
IF INSTR(1, TheInput$, ".") = 0 THEN
Filename$ = TheInput$ + ".txt"
ELSE
Filename$ = TheInput$
END IF
END IF
END SUB
The code shown in bold has been added to the subroutine and breaks down as follows. IF INSTR(1, TheInput$, ".") = 0 THEN The INSTR command will search from the first charater (denoted by the 1) of the string contained in TheInput$ variable and searches for a dot (enclosed in the quotes). Filename$ = TheInput$ + ".txt" If does NOT find it (if it finds a dot then the result would be greater than zero) then it appends a .txt to the end of the filename ELSE Filename$ = TheInput$ END IF otherwise take it just the way it is either because a filename wasn't entered by the user (so it remains with a .txt suffix) or the user added a suffix (which could be anything and might still be wrong so more error checking will be required at some point but it has been omitted here for the sake of clarity). Note the extra indentations required to make the second (or nested) IF statement more readable. And that's almost all there is to know about subroutines, for the moment at least. Until the next time we meet... |
| Written by Nick Cheesman. Last updated: 01/04/2004 Please eMail me at: nickjc@nickjc.co.uk |