Why do we need Functions? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Why do we need Functions? (/showthread.php?tid=785) Pages:
1
2
|
Why do we need Functions? - PhilOfPerth - 08-19-2022 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. RE: Why do we need Functions? - OldMoses - 08-19-2022 (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 RE: Why do we need Functions? - bplus - 08-19-2022 That's funny people at Python say, "Why do you need Subs, we don't!" RE: Why do we need Functions? - OldMoses - 08-19-2022 (08-19-2022, 02:05 AM)bplus Wrote: That's funny people at Python say, "Why do you need Subs, we don't!" Those folks are tossin' too many tuples... RE: Why do we need Functions? - vince - 08-19-2022 I use SUBs in place of FUNCTIONs when I need to return two values, ie complex numbers. here is an example of my complex math library which is a mixture of SUBs and FUNCTIONs Code: (Select All) defdbl a-z RE: Why do we need Functions? - PhilOfPerth - 08-19-2022 @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? RE: Why do we need Functions? - SMcNeill - 08-19-2022 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 RE: Why do we need Functions? - SMcNeill - 08-19-2022 As for SHARED variables be used to pass results, do you *really* want to double up on your words to remember like that?? Right now, if you want the time, you just call the function TIME$. If that function was a SUB, it'd have to look like: SUB GetTime$ SHARED Time$ Time$ = SystemTime$ END SUB Then we'd have to code with: GetTime$ 'call the sub PRINT Time$ 'use the shared variable TWO keywords to remember to do one thing.... and add in that complexity to a 100,000 line program like QB64.bas... Would anyone **really** want that?? RE: Why do we need Functions? - PhilOfPerth - 08-19-2022 Ok, thanks Steve. I can see now there are places where one would be preferable to the other. But in programmes of the (lack of) size of mine, I guess I'll still find it easier to stay with one - even at the expense of having to remember another variable's name. RE: Why do we need Functions? - OldMoses - 08-19-2022 (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 |