Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Other Programming Languages (http://www.programmingforums.org/forum38.html)
-   -   QBasic - The purpose of FUNCTION and SUB (http://www.programmingforums.org/showthread.php?t=12062)

m0rb1d Nov 30th, 2006 9:05 AM

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."
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.

Arevos Nov 30th, 2006 9:14 AM

IIRC, in QBASIC, a function returns a value as output, whereas a subroutine does not.

m0rb1d Nov 30th, 2006 9:34 AM

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?

Arevos Nov 30th, 2006 9:53 AM

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.

DaWei Nov 30th, 2006 10:48 AM

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 ()

This would presume that the get functions were something other than a trivial assignment. "possibleArg" might indicate some multiplier or other control on how much of an attribute you want to get.

m0rb1d Nov 30th, 2006 11:13 AM

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?

DaWei Nov 30th, 2006 11:48 AM

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.

m0rb1d Dec 2nd, 2006 6:13 AM

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?

Arevos Dec 2nd, 2006 6:31 AM

You probably have to give the function some arguments. See here

m0rb1d Dec 2nd, 2006 7:44 AM

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



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