Qbasicnews.com

Full Version: Subs and funcs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Whats the difference between subs and funtions?
The difference is the way results are returned:

If there are results in a sub, they must be returned as parameters, there is no result associated with the name of the sub

Code:
Define sub sum(a,b,c)
input "values to add?",a,b
sum a,b,c
print  a;" + ";b; " = ";c

sub sum(a,b,c)
c=a+b
end sub

A function name can be used in an expression because there is a result tied with the name

Code:
Define function sum(a,b)
input "values to add?",a,b
print  a;" + ";b; " = ";sum(a,b)

function sum(a,b)
sum=a+b
end function
ok.... doesent understand much but thanks anyway
Weasel,
Antoni always provides crystal-clear explanations. I'm going to try to expand on his explanation.

FUNCTIONS:
Are you familiar with the following functions that are part of the QB language, example SQR, LOG, MOD, SIN, TAN, etc. These are called INTRINSIC functions. When you say
X = SQR(69)
the intrinsic or internal function SQR is called, it calculates the square root of 69, and returns the result, which in turn is stored into X.

There are also USER-DEFINED functions. These are defined by you the user. Let's say you wanted a square root function, but you want the result to always be an integer and rounded. You could then write a function called SQRINT for example, as follows:

FUNCTION SQRINT (V)
SQRINT = INT(SQR(V)+.5)
END FUNCTION

The code for this function is included at the end of your program. However, you are now free to use it anytime in your code just like it was part of your QB language. To use it, do the following:
X = SQRINT(69)

SUBS:
I'm sure you're familiar with subroutines. Well SUBS are fancy subroutines that allow you to call the SUB with parameters, and to receive one or more parameters back from the SUB. One of the big advantages is that all the variables used in the SUB are LOCAL, that is, they will not conflict with any variables used by the rest of the program. If you ned a more detail explanation of LOCAL variables, let me know.
*****
oh... now i understand.... subs makes it more easy to find in the code if it is an big program and functions... nah i dont know...
well well, subs is like this

Code:
DECLARE SUB savegame()

INPUT "Wanna save game?", s$
IF s$ = "yes" THEN GOSUB savegame

SUB savegame()
Code for saving game
END SUB
Almost right, Weasel. You did a GOSUB to the sub. Gosubs are for subroutines which have a RETURN at the end.

To invoke the SUB, you should do:

IF s$ = "yes" THEN savegame

or you can do:

IF s$ = "yes" THEN CALL savegame

*****
weasel...

SUBS and FUNCS are both blocks of code that can take parameters. both perform some arbitrary operations. in that essence, both subs and funcs are IDENTICAL.

the only difference between subs and funcs is that subs do not return a value, while funcs do.

for example, the command CLS is a sub. it clears the screen ("performs some arbitrary operations") and does not return any value.

the command SQR, on the other hand, returns the square root of the given number.

generally, subs are used as statements, and functions as expressions. for example:

CLS
a = SQR(49)
PRINT a

CLS and PRINT are both subs ("statements") , while SQR is used as part of an expression.


another example:

FUNCTION AngleSin# (x as double)
AngleSin# = SIN( x *3.141592653589/180)
END FUNCTION

PRINT AngleSin(90)









[Flexibal>