08-19-2022, 06:28 AM
The difference is rather simple:
SUBS **do** something. FUNCTIONS **return** something.
CLS, PRINT, BEEP -- these are all subs as they do something.
_KEYHIT, _MOUSEINPUT, INKEY$ -- these are all functions as they return something.
Now, you talk about returning values via parameter, and this is true -- but only in limited scope as the parameter types must match.
SUB foo (x AS INTEGER) will pass the value of x to the reference variable ONLY if it's an integer. Pass it a byte, long, constant, single, or anything else, and that value won't pass back.
FUNCTION, on the other hand, always returns a given type value.
FUNCTION foo% (x) -- pass it a byte, long, single, const, literal... doesn't matter! You'll still get back an integer value!
**************
As for the biggest difference between the two, I'd say that's obvious with this little example:
PRINT TIMER
Print is a SUB. Timer is a function. Yet, the two are working together here on the same line!! Try that with any two SUBs and see what happens!
PRINT CLS
SUBS **do** something. FUNCTIONS **return** something.
CLS, PRINT, BEEP -- these are all subs as they do something.
_KEYHIT, _MOUSEINPUT, INKEY$ -- these are all functions as they return something.
Now, you talk about returning values via parameter, and this is true -- but only in limited scope as the parameter types must match.
SUB foo (x AS INTEGER) will pass the value of x to the reference variable ONLY if it's an integer. Pass it a byte, long, constant, single, or anything else, and that value won't pass back.
FUNCTION, on the other hand, always returns a given type value.
FUNCTION foo% (x) -- pass it a byte, long, single, const, literal... doesn't matter! You'll still get back an integer value!
**************
As for the biggest difference between the two, I'd say that's obvious with this little example:
PRINT TIMER
Print is a SUB. Timer is a function. Yet, the two are working together here on the same line!! Try that with any two SUBs and see what happens!
PRINT CLS