Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2006, 8:05 AM   #1
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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.
Attached Files
File Type: txt Tutorial 2.txt (8.2 KB, 22 views)
m0rb1d is offline   Reply With Quote
Old Nov 30th, 2006, 8:14 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
IIRC, in QBASIC, a function returns a value as output, whereas a subroutine does not.
Arevos is offline   Reply With Quote
Old Nov 30th, 2006, 8:34 AM   #3
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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?
m0rb1d is offline   Reply With Quote
Old Nov 30th, 2006, 8:53 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Nov 30th, 2006, 9:48 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old Nov 30th, 2006, 10:13 AM   #6
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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?
m0rb1d is offline   Reply With Quote
Old Nov 30th, 2006, 10:48 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 2nd, 2006, 5:13 AM   #8
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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?
m0rb1d is offline   Reply With Quote
Old Dec 2nd, 2006, 5:31 AM   #9
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You probably have to give the function some arguments. See here
Arevos is offline   Reply With Quote
Old Dec 2nd, 2006, 6:44 AM   #10
m0rb1d
Newbie
 
Join Date: Nov 2006
Posts: 19
Rep Power: 0 m0rb1d is on a distinguished road
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
m0rb1d is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC