|
Local | |||
Local <.varname.> | |||
Parameters: NONE | |||
Returns: NONE | |||
Local declarations like Global declarations, affect the visibility of variables. By default, when a variable is used without being previously declared as Local, Global or Static, PlayBASIC will automatically make the variable local to the scope it was declared within. While It's considered good programming practice to always declare your variables, it's not a requirement. Thus variables used within the program main, are local to the main program and are not visible from within your Functions & Psub's. Likewise, any variables created within your functions / subs will also only be visible within the Function / Psub (the scope) they were used within. This is often referred to as encapsulation, as variables will only exist within the scope their defined and are not accessible program wide. This protects your variables from being harmed by a different scope accidentally. However while variables default to local scope automatically, without manual declaration, there are occasions when you will need manually force a variable to local. To ensure that PlayBASIC uses the correct local declaration of a particular variable name (within a function), over a global variable that shares the same name. This occurs because when the compiler sees a variable within a function or sub, it first checks for any global instances of this variable name, if one exists, the global instance is the one that's used. If not, it defaults to local. So if the situation arises that you wish to use a variable name within a function that is also declared as global (in some other part of your program), you can override this with the Local directive. Local supports two basic forms, You can simply define a single local or list of locals using comas to separate them, or define a local with an assignment. i.e.
Note: Variables defined as local will have precedence over any variable of the same name, that was defined in main program as global. FACTS: * Local variables can only be defined within Functions * Local can handle assignments. I.e. (Local Score=1000) * Local can handle lists by using coma's between them. I.e. (Local Monday,Tuesay,Wednesday,Thursday,Friday) * Local Variables have precedence over any variable of the same name that was defined in main program as global. Mini Tutorial : This simple examples is meant to shows how Local declarations can override external global variables declarations.
In the output, You'll notice that he global variable is only visible from within the Show_Global_Visible function. While in the Show_Local_Visible() function the code is creating a local variable of the same name and showing it. Functions/Psub have their own sets of variables completely, so while the MyVaribale variable name is used within the main program and the function, they are two entirely separate variables. Who share a name, but nothing else.
|
Related Info: | Constant | Dim | Explicit | Function | Functions&Psub | Global | Psub | Static | Variables : |
|
|||||||||||||||||||||||||||||||||||||||
(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com |