Why do we need Functions?
#10
(08-19-2022, 05:40 AM)PhilOfPerth Wrote: @oldmoses: I can see that this can be a way to achieve what you wanted, but it can be done with a Sub as well, with the same result and about the same amount of coding. Why try to ride two ponies when one can take you anywhere?

My point in my example was that I had to create a proxy variable in order to do the same thing with a SUB. If there's already an existing variable to modify, then that's fine and in many cases even optimal, but often you need a number for a quick IF...THEN check and don't need to keep it, or you need to place conditions on a particular part of an expression. Don't muddle up the code with extraneous variables and multiple lines of unnecessary code that no one is going to want to wade through to look at if you have problems.

It's best to become accustomed to using the right tools for the task before that happens. Clean and concise code will follow naturally.

If you need a number in a computation that you don't need to keep for something else, but you find yourself doing the same thing over and over, you're going to want to use the function.

Even my own example can benefit from additional optimization thanks to function use
Code: (Select All)
a% = 2
b% = 4
PRINT a% + b% + MultF(a%, b%) ' don't need the c% variable after all
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:
Reply


Messages In This Thread
Why do we need Functions? - by PhilOfPerth - 08-19-2022, 01:00 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 01:44 AM
RE: Why do we need Functions? - by bplus - 08-19-2022, 02:05 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 02:12 AM
RE: Why do we need Functions? - by vince - 08-19-2022, 03:40 AM
RE: Why do we need Functions? - by PhilOfPerth - 08-19-2022, 05:40 AM
RE: Why do we need Functions? - by OldMoses - 08-19-2022, 12:58 PM
RE: Why do we need Functions? - by SMcNeill - 08-19-2022, 06:28 AM
RE: Why do we need Functions? - by SMcNeill - 08-19-2022, 06:42 AM
RE: Why do we need Functions? - by PhilOfPerth - 08-19-2022, 08:15 AM
RE: Why do we need Functions? - by bartok - 08-19-2022, 01:36 PM
RE: Why do we need Functions? - by mnrvovrfc - 08-20-2022, 11:59 AM
RE: Why do we need Functions? - by bartok - 08-20-2022, 01:54 PM
RE: Why do we need Functions? - by mnrvovrfc - 08-20-2022, 11:56 AM
RE: Why do we need Functions? - by TempodiBasic - 08-20-2022, 04:52 PM
RE: Why do we need Functions? - by bplus - 08-20-2022, 04:54 PM



Users browsing this thread: 9 Guest(s)