![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
QBasic - The purpose of FUNCTION and SUB
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."
PRINT
PRINT "1. Attack the monster."
PRINT "2. Run away screaming for your mother."
PRINT
INPUT "What will you do? 1 or 2.", choice%
IF choice% = 1 THEN
monsterStats ' this is the name given to the function
PRINT "You attack the monster with your weapon."
IF attack% >= mobEva% THEN
damage% = attack% - mobEva% + weapBonus%
mobHP% = mobHP% - damage%
END IF
PRINT "You hit the monster for "; damage%; "damage."
IF mobHP% = 0 THEN
PRINT "You have killed the monster."
exp% = mobExp% + exp%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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
IIRC, in QBASIC, a function returns a value as output, whereas a subroutine does not.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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) monsterEXP = getEXP () monsterGOLD - getGOLD ()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
So, if I understand this correctly, the functions in your example would be:
FUNCTION getHP RANDOMIZE TIMER getHP = charHP% - (RND * 10) + 1 END FUNCTION This 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? |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
For the life of me, I cannot get FUNCTIONS, or SUBS to work. I am using them as follows:
PRINT "blah blah" PRINT "more blah blah" PRINT "even more blah blah" getClass 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? |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
You probably have to give the function some arguments. See here
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Nov 2006
Posts: 19
Rep Power: 0
![]() |
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
PRINT "What would you like to do?: "
PRINT
PRINT "1. Attack "
PRINT "2. Run Away"
PRINT
INPUT "Your choice?", choice%
IF choice% = 1 THEN
CALL combat(hit%)
CALL defense(def%)
ELSE GOTO 190
END IF
IF hit% > def% THEN
CALL damage(dam%)
ELSE PRINT "You missed."
END IF
mobHP% = mobHP% - dam%
IF mobHP% <= 0 THEN
GOTO 185
ELSE GOTO END
SUB combat(hit%)
RANDOMIZE TIMER
hit% = INT(RND * 20) + 1
END SUB
SUB defense(def%)
def% = eva% + armBonus%
END SUB
SUB damage(dam%)
dam% = hit% - def%
END SUB |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|