• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Subroutines in QBasic

Not sure if this is the right place or not, but I noticed in software, programming and games it was mostly help on using it not creating it.

How do you share a variable inside of a subroutine with its main module in QBasic?

I know how to do it the other way around by declaring the variable as COMMON SHARED, but I can't figure out how to share it to the main module.

Here is my subroutine for solving a quadratic equation...

--------------------
SUB QUADEQ
2 RESTORE
PRINT
PRINT "For the form (ax"; CHR$(253); " + bx + c = 0) enter a, b, and c in the following prompts"
PRINT
INPUT " a: ", a
PRINT
INPUT " b: ", b
PRINT
INPUT " c: ", c
LET disc = (b ^ 2) - 4 * a * c
SELECT CASE disc
CASE IS < 0
LET real = -b / (2 * a)
LET imag = SQR(-disc) / (2 * a)
PRINT
PRINT
PRINT USING "The answer is ##i and ##i"; real + imag; real - imag
CASE IS > 0
LET x1 = (-b + SQR(disc)) / (2 * a)
LET x2 = (-b - SQR(disc)) / (2 * a)
PRINT
PRINT
PRINT USING "The answer is ## and ##"; x1; x2
CASE IS = 0
LET x = -b / (2 * a)
PRINT
PRINT
PRINT USING "The answer is ##"; x
END SELECT
PRINT
PRINT
33 INPUT "Would you like to try again (Y/N)"; choice$
SELECT CASE choice$
CASE "y", "Y"
GOTO 2
CASE "n", "N"
SYSTEM
CASE ELSE
GOTO 33
END SELECT
END SUB
--------------------

I want to share choice$ with the main module so I can use it's value to dump the user back to the main interface screen with an IF.... THEN.... statement.

Any ideas?
 
Never mind guys im stupid! 😕

I just realized I dont even need to have a choice at the end of the subroutine. I already have a choice at the end of the main module so I can just end the subroutine! DOH!!!
 
I guess while im thinking about it though....

Does anybody know the largest possible number that QBasic can compute?

I noticed when I use some large numbers with a couple of my formulas that intergrate exponetiation I get overflow errors.

Mods, feel free to delete this if its not HT.
 
I noticed when I use some large numbers with a couple of my formulas that intergrate exponetiation I get overflow errors.
Try using variables with a higher precision e.g. instead of int try to use something like long int if it exists. Check the online help.
 
Im already using double precision variables. I guess what I need to know is the highest number that QBasic can use.

This is where the problem occurs.

...

INPUT "Input 2 numbers: ", num1#, num2#
PRINT "The answer is"; num1# ^ num2#
...

Just a simple exponetiation problem, but If the answer is too big I get an overflow error at the PRINT line. Since QBasic has a limit to the number it can use I need to know what it is so I can create an IF... THEN... statement to get around the problem.
 
Most languages use the IEEE format for doubles, which (pasting from VB help)

The highest positive value of a Single data type is 3.402823E+38, or 3.4 times 10 to the 38th power; the highest positive value of a Double data type is 1.79769313486232D+308, or about 1.8 times 10 to the 308th power. Using D to separate the mantissa and exponent in a numeric literal causes the value to be treated as a Double data type. Likewise, using E in the same fashion treats the value as a Single data type.

QBasic is so old it might be dividing its 64 bits up differently (giving more or less bits to the exponent) but you can easily try something slightly smaller then slightly larger than the above and see whether it errors on you.
 
LMAO

LOL

QBasic 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂

And GOTO 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂 🙂
 
It's been a while since I've seen any QBasic.... 🙂

For reference, pimpbot5000, people usually try and avoid goto's at all costs because alot of people see their use as being bad practice.

For the data types, I would look in the QBasic help and see if it tells you how many bits are used for the data types you've chosen. Simply saying int or long int doesn't really have alot of meaning anymore, atleast not in my opinion. When I started learning C++ with Turbo C++ in DOS, int were 2 bytes, and long ints were 4. I wrote alot of data structures that I attempted to port to Visual C++ later, and ended up having problems because the Visual C++ compiler counts int as 4 bytes and not 2.

If you can't find the information in the documentation, you can try some simple tests to see how large a value you can use. Write a simple program that takes a value as input and then prints the value back to you. Input values on byte boundaries, such as 2^16, and then try 2^16 + 1, see if the result from the second input is negative. Not exactly the quickest way, but it might give you a better impression of what is going on. If you get an overflow error on the second input, you'll know right away that 2^16 is the highest value you can store in that variable, and that the variable is 2 bytes in length. This might get a little more tricky with floating point values, but I think it should still work.

Anyhow, hopefully that helps a little, if not, sorry. 🙂 I just thought I'd throw in my 2 cents.
 
Back
Top