I'll take a shot at these ...
1. What type are variables initialized by STORE or '='? PRIVATE?
If you don't explicitly declare the scope, any variables you create are private, which means they're visible in the routine that creates them, and any lower level routines (unless the lower level routines themselves declare the variables as private, in which case it becomes a different variable).
Actually, it's slightly more complicated than that: PRIVATE doesn't actually create a variable; it simply says that any undeclared variables with the specified names that are created in the routine will be private. But don't worry too much about that.
2. What is the advantage of declaring a variable as LOCAL and hiding it from the lower level?
A big advantage: it helps to keep your procedures and functions self-contained. You can call whatever functions you like without having to worry about what variables they use, or whether there's likely to be a name clash. That in turn makes coding and testing easier, and it helps to avoid hard-to-find bugs.
3. If I have, say, a counter (nCntr), which will be changed in a lower level function, what is the proper way to declare it in the calling routine:
You can do that by making it private in the caller (and
not making it private in the lower level). But it's generally advisable to keep away from privates.
A better way is to pass the variable to the lower level as a parameter - and to pass the parameter
by reference. That means that both routines will see the same value, and any changes that the lower level make will be passed back to the caller.
But I'd question why you want to do this. If you want a function to transform a value in some way, it's better for the function to receive the value as a parameter (passed in the normal way, that is, by value, not by reference). The function would then return a new value to the caller.
A quick example:
Code:
* main procedure
LOCAL lcSSN
USE SomeTable
GO TOP
lcSSN = SomeTable.SSN
* Convert SSN to printable format
lcSSN = FormatSSN(lcSSN)
* etc etc.
FUNCTION FormatSSN
* Receives a string representing a social security number.
* Adds dashes between the elements; pads with leading
* zeroes.
LPARAMETER tcStr
LOCAL lcWork
lcWork = PADL(tcStr, 9, "0")
RETURN LEFT(lcWork, 3) + "-" + SUBSTR(lcWork, 5, 2) + ;
"-" + RIGHT(lcWork, 4)
ENDFUNC
The best advice it to always to declare your variables as local, unless you've got some overriding reason not to (for example, passing variables to reports, or using them in the ON SELECTION clause of a menu - but those are unusual cases).
Mike
__________________________________
Mike Lewis (Edinburgh, Scotland)
Visual FoxPro tips, advice, training, consultancy
Custom software for your business