RE:
@madscijr longer post above who I am quoting
"If I want a variable to retain its value outside of a function's scope, I'll just use a global variable, and declare them all together at the top of the entire program (right after the global Const & Types). This makes it easy to know what global variables are declared, without having to look inside all the functions & procedure to see what's shared. For me that keeps it simpler."
global variables are Dim Shared at top of code, sure. Also make first letter capital as sign that it is Shared. Just like our Keywords in QB64.
"One thing that bugs me about shared variables inside a routine is, how do you know the function is being called the first time so you know to initialize its value the first time? Seems a little ambiguous. Do you have to declare a second boolean variable called something like bInitialized to track whether the function has been run at least once to know the shared vars have been initialized? Seems messy, so if I'm going to use variables that retain their value, I'll just declare them globally and initialize them all at the start of the program (or if I really want to keep my routines portable, don't use global variables at all, and just pass everything as parameters). "
I use:
Static BeenHere as Long
If BeenHere = 0 then 'never been called yet
' initailize a bunch of stuff also under Static to preserve values inside subroutine
BeenHere = -1 ' no longer a virgin
end if
"Also, when declaring a variable as shared inside a routine, is the variable scope unique to that routine? So if I have a function MyFunction1 with a shared variable x%, and another routine MyFunction2 with its own shared x%, are they the same or does each function have its own instance of x%? (If I was at my PC, I could write a test prog and find out, but I am not.)"
I almost never do that. But according to Wiki it is Shared only with Main not other routines, which I think answers the question you had.
"To keep things safe for parameters you just want to send "by value", I just do the simple thing and declare local copies, copy the parameter values to those, and only reference the actual parameter variables if I want to return a new value."
Yep, that's right. Make copies and alter those values not the calling arguments unless you intend on changing their values.
b = b + ...