Qbasicnews.com

Full Version: Subs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I need to know how to get a variable from a SUB into other subs or the main program, I'm pretty sure this is a newbie question...I though that it was "RETURN VarName$".
Try this:

Code:
DECLARE SUB MySub(a%, b%)

MySub 7, hello%

PRINT hello%

SUB MySub(a%, b%)
   b% = a%
END SUB

Variables are passed by reference, so the value they get inside the function is the value they get...

Let's explain it step by step (I'm sleepy and english doesn't flow correctly now Big Grin):

1.- MySub 7, hello%

This one calls to MySub, passing a "7" and a variable "hello%". In MySub, a% becomes 7 and b% becomes "hello%", it is somewhat bound.

2.- In the sub, you do "b% = a%", so b% = 7, and as hello% is bound to b%, hello% = 7.

3.- You exit the sub, PRINT hello% and it displays "7".

Aw someone ellaborate on this, I'm terribly sleepy Big Grin Wink (But I hope you understood).
:???: :-?

don't you call subs like this: CALL SubName(arg1, arg2) ?

and isn't hello% a variable name? I thought that % means integer or float-point in qbasic...
btw whats the difference between a function and a sub in qbasic?
a SUB jsut executes stuff, while a FUNCTION returns something...

Code:
DECLARE SUB PrintText(row AS INTEGER, col AS INTEGER, text AS STRING)
DECLARE FUNCTION AddIt%(var1 AS INTEGER, var2 AS INTEGER)

PrintText 1, 1, "Lalala"

n% = AddIt%(1, 1)

SUB PrintText(row AS INTEGER, col AS INTEGER, text

locate row, col
PRINT text

END SUB

FUNCTION AddIt%(var1 AS INTEGER, var2 AS INTEGER)

AddIt% = var1 + var2

END FUNCTION

Oz~
Oooooh, so that's the answer to my first, and last question right?
Variables are passed by reference or value depending on whether they have brackets around them, just to muddy the waters a little.

So CALL subr (i, r) is slightly different from subr i, r

*jeremy, ignore this Wink*
Why doesn't anybody metioned shared,

Type:
DIM SHARED Variable

Now the value of variale is carried over every sub.
Quote::???: :-?

don't you call subs like this: CALL SubName(arg1, arg2) ?

and isn't hello% a variable name? I thought that % means integer or float-point in qbasic...

For a simple explaination for now, you can do either:

[syntax="qbasic"]CALL subname(arg1, arg2)

or

subname arg1, arg2[/syntax]

As for your other question,

a SUB carries out a requested routine, as suggested by the name Subroutine. A FUNCTION will return a value calculated by the values you put in.

for example you could have a function like this:

[syntax="qbasic"]DECLARE FUNCTION add(a, b)

result = add(3, 4)
PRINT result
'output should be 7

FUNCTION add(a,b)
add = a + b
END FUNCTION[/syntax]
Quote:Why doesn't anybody metioned shared,

Type:
DIM SHARED Variable

Now the value of variale is carried over every sub.

Because that's not what he asked Smile

Let's explain reference and value.

You know that (roughly) a variable is some kind of bind between an identifier (the variable name) and a value which is (most likely) somewhere in the DATA segment of QB. So there is some kind of "pointer" that points to where the value is in memory, and this pointer is associated to the identifier.

For example, if you create the variable y% in QB with this code:

Code:
y% = 7

QB looks where in the DATA segment is space available, it puts a 7 on it and binds a pointer to the identifier y%, so when you use "y%" he knows where to look in memory to retrieve the value.

When you do:

Code:
z% = y%

What QB does is looking at the pointer associated to y%, going to that position in memory, get the value, look for some free space, associate a pointer to that new space to z%, and put the value previously read from y% into that memory space. Now z% is a new variable which has the same value.

When calling SUBs or FUNCTIONs things are slightly different. If nothing is specified, i.e. you have a

Code:
DECLARE SUB mySub (a%, b%)

SUB mySub (a%, b%)
   ' some code
END SUB

parameters are passed by reference. What does that mean? It means that what's passed to the SUB is the pointer, and not the value itself.

This means that if you have a variable myVar1% and another variable myVar2%, when you call the sub:

Code:
mySub myVar1%, myVar2%

Inside the sub, a% points to the same memory address as myVar1% does, and b% points to the same memory address as myVar2% does. Why? Because parameters are passed by reference: values are not copied into a% and b%, but a% and b% are made to point to the same memory addresses that already exist and are assoc. to the variables that are passed to the SUB call instead.

That means that if you assign values to a% and b% inside the sub, those values are written in the memory addresses of myVar1% and myVar2%, thus those changes are "visible" outside the SUB.

You can also change the way the parameters are passed. You can force QB to pass them by copy (or by value). That means that instead of passing a reference (the pointer), QB creates new variables and copies the values themselves, so if you change them inside the SUB they don't affect to what happens outside. You need BYVAL then in the sub declaration:

Code:
DECLARE SUB MySub (BYVAL v%, BYVAL c%)
Pages: 1 2