![]() |
QBasic - The purpose of FUNCTION and SUB
1 Attachment(s)
Continuing along with this tutorial I have been working on, it explains subs and functions. From the examples given, it looks to me, as if they do the same things.
I have been working on a text based RPG. I figured, making it will help me in retaining what I have learned so far. It came to a point where I could use the first function/sub, but, it is slightly confusing as to the manner in which you use them. According to the tutorial, it should go something like: :
PRINT "BLahblah goes here. Big scary monster trying to eat your head."Well, you get the idea. After typing this out, I have realised, there is a couple more things I could have used funtions for. But, for the sake of keeping my sanity, I will revert to my original question. Would SUB have worked just as easily as FUNCTION? And yet again, an epiphany. Putting 2 and 2 to together, I'll ration, SUB = SUB FUNCTION? Following this rational, SUB should be used inside a FUNCTION? I may have successfully confused someone people, including myself, so I will include the tutorial chapter that I am reading. It gave no example of a SUB being used inside a FUNCTION. Biggest problem I'm having I suppose, is that what I assume should work, does not. |
IIRC, in QBASIC, a function returns a value as output, whereas a subroutine does not.
|
After rereading that tutorial, it seems SUB's DO something, and functions return a value.
So, I suppose the actual question I'm looking for an answer to is... Im wanting to generate 3 values, and assign them to variables. This will need to be done repeatedly throughout the program. Following the SUB doing something, and functions returning a number, it would seem that it's possible to achieve this by using FUNCTION. But, can FUNCTION give the value of 3 things at once? Such as, the monsters HP, the monsters EXP value, and the monsters Gold value? Or, would I have to use 3 seperate functions? |
You could DIM an array and return that. I believe there's also a way to DIM a data structure, similar to structs in C, but it's been years since I touched QBASIC, I'm afraid. To use an analogy, QBASIC akin to a flint knife in a world where iron has become commonplace. Tasks which are trivial in other languages are sometimes difficult to do within QBASIC's limited featureset.
|
At this stage, I believe you'd be fine using separate functions. You might not always want to do all three things at once, anyway. Pseudo-code example:
:
monsterHP = getHP (possibleArg) |
So, if I understand this correctly, the functions in your example would be:
:
FUNCTION getHPThis would ensure that the monsters HP is not always the same, and that they would scale appropriately as the character leveled up. Was this what you were expressing? |
Something like that. Anything you need to do repetetively, that isn't trivial, should be modularized (become a function or sub). With a little thought, you will see that such things can be very useful. The best and most utilitarian can become building blocks that you use to assemble other programs from. Get 'em off the shelf and bolt 'em on, so to speak.
|
For the life of me, I cannot get FUNCTIONS, or SUBS to work. I am using them as follows:
:
PRINT "blah blah"I've tried about a million times, and I always seem to get an error. In this example, it will print what I have inside the SUB getClass, but it doesn't seem to recognize the INPUT command. It just prints it instead, and continues on with the rest of the code. Same thing is happening with FUNCTIONS, only, I get mismatch errors, or duplicate definition errors. I thought for a while, I was just using it wrong, but, I am not sure anymore. Any idea what I am doing wrong? |
You probably have to give the function some arguments. See here
|
Hm, seems the more I learn, the more confusing it gets, and the more questions I end up having. Such as,
Can you use a variable? Combat - :
180 |
| All times are GMT -5. The time now is 1:38 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC