PDA

View Full Version : Solved: Global Variables vs Function Return values



CBrine
11-24-2006, 02:25 PM
I'm wondering if someone can let me know what the best method regarding global varilable usage vs Function Return values? Is there a reason to use one over the other? Is there a standard practice regarding both? Is one more effeicient then the other?
I'm not talking declaring all varialbles as global, only ones that are used outside of the scope of the subroutine.
I've done some searches and haven't found anything pretaining to this specfic topic, or with anyone who's judgement I would trust.

Thanks in advance.

Cal

PS- I'm looking at this from an Excel VBA perspective, although I'm sure it will apply to VB and programing in general as well.

Norie
11-24-2006, 02:41 PM
Cal

Any chance of some examples of what you mean?

I've seen plenty of discussions over the years regarding this sort of think.

I think a lot of people advocate not using Global/Public variables wherever possible.

Obviously they have their uses under certain circumstances.

Bob Phillips
11-24-2006, 05:48 PM
My approach is to use global vbariables as little as is possible. This means that I will pass values to a function as parameters rather than have globals, and I will use ByRef to have updateable arguments, and return values from a function rather than use a global.

I was always taught to restrict the scope of variables as much as you can. This means as few module level variables, as few globals as possible.

As an aside, in my production code, I rarely use subs, invariably use functions.

CBrine
11-25-2006, 06:38 PM
Here's a couple of Examples.

I've created a simple log class that creates a txt file and allows me to write to it. I usually define this a global varilable, so that I have access to it in each of my subroutines.
I also created a global array to hold filenames in array's that I used in another subroutine.
I think with the log it makes sense as a global array, but I figure the filearray should have been passed as a parameter to the other subroutine, then each filename passed as parameter to the next sub. I didn't do it that way, but thinking about it now that would have made more sense. I think from a readability standpoint for the code, it would be more understandable that way.

CBrine
11-25-2006, 06:40 PM
That's the second time the board has posted stuff while I was typing it????? Just typing away and the next thing I know I got multiple posts? Not sure what key I managed to hit???

johnske
11-25-2006, 06:58 PM
Hi Cal,

I deleted the 2nd one for you. Never had it happen to me personally, but I've noticed the odd duplicate by others here and there - maybe it's browser related thing?

Regards,
John :)

EDIT: out of curiosity - I use IE6, what are you using?

CBrine
11-26-2006, 01:42 PM
I'm using Flashpeak Slimbrowser, which supposedly uses the IE backbone with a suped up interface. I'm a touch typer, so I'm pretty sure I didn't hit and keys other then the ones I was supposed to? It's always a possiblity I hit some control combo I wasn't aware of.:dunno

Cal

Zack Barresse
11-26-2006, 05:07 PM
I agree with Bob. I'll use them, but not very often. I like to keep things compartmentalized as much as possible. I pass a lot of variables between sub routines and use a lot of functions. There is the occasion where I'll need to store values in between the run-time of multiple routines. Public variables can come into play here, or I may find another way to store the data.

I think the bottom line is there is no concrete answer. The less the better (IMO), but I don't know if there is a definite line of black and white, there is always some gray. :)

Bob Phillips
11-27-2006, 01:55 AM
I agree with Bob. I'll use them, but not very often. I like to keep things compartmentalized as much as possible. I pass a lot of variables between sub routines and use a lot of functions. There is the occasion where I'll need to store values in between the run-time of multiple routines. Public variables can come into play here, ...

... or use classes

CBrine
11-27-2006, 07:48 AM
Thanks for the advice guy's. It seems to boil down to "Don't use global varailables unless you have no other choice, and there is always another choice"":-)

Cal

Umm..Ahh...Now if I can just figure out how to make this thread solved.
:-)

Bartek
11-27-2006, 12:38 PM
Hi,


I'm wondering if someone can let me know what the best method regarding global varilable usage vs Function Return values? Is there a reason to use one over the other?
VBA global variables in addition of simply being available to all other procedures have one additional feature: they remain in memory until you alter/reset VBA project.

Even if macro finishes running the variable will still occupy the memory. In case of huge variables it may be easily visible in Task Manager / Processes (memory used by Excel.exe). In some cases it may be an advantage - keeping some data in global variable (much faster access, computations) while working with excel worksheet.

Zack Barresse
11-27-2006, 03:35 PM
... or use classes
Yes! I love classes. :)