![]() |
Use of 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: Use of Functions (/showthread.php?tid=3699) |
RE: Use of Functions - SMcNeill - 05-21-2025 (05-21-2025, 09:12 AM)bplus Wrote: To illustrate further the difference between a Function and an equivalent Sub: Note that this is... wrong? Misleading at the very least. Functions behave *exactly* as Subs do with regards to passing values and changing their arguments. I'm not certain what "unless you tell them to" refers to, but if types match and values change inside the function, they'll change outside it too. x = 1 PRINT foo (x) PRINT foo (x) PRINT x FUNCTION foo (x) foo = 2 * x X = x + 1 END FUNCTION ^ Prints 2 (x = 1), then 4 (x = 2 after the first call), then 3 (final value of x) RE: Use of Functions - bplus - 05-21-2025 Most times when I use x in f(x) I don't want to change x, that could mess things up badly! Here's how variable changing may be misused with a sub Code: (Select All) For x = 1 To 10 With function it's clear x wont get messed up: Code: (Select All) For x = 1 To 10 RE: Use of Functions - bplus - 05-21-2025 SMcNeill: "I'm not certain what "unless you tell them to" refers to" It refers to exactly what you showed in your example: FUNCTION foo (x) foo = 2 * x X = x + 1 ' <<< here Steve told the function to change the variable! not normally done with functions END FUNCTION RE: Use of Functions - bplus - 05-21-2025 I think the best way a beginner might look at a function is that a function works just like a formula, feed it arguments and it RETURNS a single result and you don't usually want to change the value of the arguments while processing the result. RE: Use of Functions - SMcNeill - 05-21-2025 The best way to avoid changing values is just to assign to a temp variable. FUNCTION foo (tempX) X = tempX ... do whatever with X END FUNCTION 100% safe from changing the value, if you only assign it to a temp variable. RE: Use of Functions - bplus - 05-21-2025 I am afraid Phil is mistaking variable changing as something a sub or function mainly does. A sub acting as a function has to change an incoming variable to outgoing one but a function doesn't have to do that at all! RE: Use of Functions - TempodiBasic - 05-21-2025 Hi I want to share these informations about SUB and FUNCTION 1. you can declare and use only Function for the type of data that you can declare using a suffix as you can see from these wikipage Data Types and their suffix. So you cannot (for now) create a FUNCTION returning a _MEM value or an Array of what_ you_ desired_dimensions or an UDT. 2. SUB and FUNCTION get the parameters for reference and not for value... so if you modify the variable into the procedure (SUB/ FUNCTION) you get back the variable with the modified value. It works for any kind of parameter, so you can return an array and a _MEM and an UDT modified from a FUNCTION (and a SUB). 3. if you don't want to change value of variables passed as parameter you can use 2 ways: 3.1 you can pass the parameters as value ... each parameter has been typed sorrounded by () Function: the 6th point of descriptions or 3.2 you can declare a duplicate of the parameter into the procedure (SUB/FUNCTION) and you assign to these last the parameters passed. So you work with local copies of the parameters into the procedure. RE: Use of Functions - mdijkens - 05-21-2025 Example showing difference passing parameter variables by reference or by value: Code: (Select All)
RE: Use of Functions - Dimster - 05-21-2025 Hi Phil Like you, I find I use Subs a lot more than Functions but with my coding I often find I'm dealing with Scientific Notation and a Function here has really sped up things. I'm sure there is likely a better Function routine for handling Scientific Notation but here's an out line of my favorite Function. Code: (Select All) Dim Shared Number, x, y And if this looks familiar to either Steve or Mark, or another on this forum, then yes, I likely did find it here and thank you for same. RE: Use of Functions - Kernelpanic - 05-21-2025 Functions can, of course, also be passed to procedures (subs) as arguments. Just as functions can be passed to functions as arguments. This is usually done to save code lines. However, for people unfamiliar with this, it's difficult to understand. Code: (Select All)
Functions can also produce more than one output, but only return one. (Uploading an image isn't working right now.) Code: (Select All)
It looks pretty confusing, but it works. |