(08-19-2022, 01:00 AM)PhilOfPerth Wrote: I read that there is only one difference between Subs and Functions: a function returns a value, while a Sub doesn't. But as far as I see it, you can use Subs everywhere that you could use a Function. If I call a Sub, with variable parameters, I can work on those variables and (as long as they're Common Shared) I get the changes back in the main prog. Is there some other subtle difference? if not, it seems like Functions are an unnecessary item.
True that you can change values of parameters provided to SUBs, and I often do, but the main difference would be that a FUNCTION can be used in an expression directly to provide a value for use in the expression. A SUB will not do that.
Example:
Code: (Select All)
a% = 2
b% = 4
c% = a% + b% + MultF(a%, b%)
PRINT c%
d%=a%+b%+mults k%,a%,b%
PRINT d%
MultS k%, a%, b%
PRINT k%
FUNCTION MultF (var1%, var2%)
MultF = var1% * var2%
END FUNCTION
SUB MultS (result%, var1%, var2%)
result% = var1% * var2%
END SUB
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
sha_na_na_na_na_na_na_na_na_na: