#167388 - 2006-09-10 08:12 PM
Re: disposable variables and functions
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
I think the trick is in DIM, to make them local and not global Like you typed it they will be global. Code:
Break on Dim $SO $SO = SetOption("Explicit","On") $SO = SetOption("NoMacrosInStrings","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("WrapAtEOL","On")
? Function1 ?
Function Function1() Dim $ $ = T ? "1st $ in F1 = " $ ? Function2() ? "2nd $ in F1 = " $ ? EndFunction
Function Function2() Dim $ ? "1st $ in F2 = " $ ? $= Z ? "2nd $ in F2 = " $ ? EndFunction
Code:
Break on
? Function1 ?
Function Function1() $ = T ? "1st $ in F1 = " $ ? Function2() ? "2nd $ in F1 = " $ ? EndFunction
Function Function2() ? "1st $ in F2 = " $ ? $= Z ? "2nd $ in F2 = " $ ? EndFunction
That's why I like the Explicit Option
|
Top
|
|
|
|
#167389 - 2006-09-10 08:27 PM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Explicit is nice until such time as you begin using KiXForms, then all of sudden you have X number of dims to suddenly create.
Such is the life of scripter
|
Top
|
|
|
|
#167391 - 2006-09-10 10:28 PM
Re: disposable variables and functions
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Maybe when using KiXforms, just before and after the layout made with KiXforms designer, you can do something like Code:
dim $OldExplicitOption $OldExplicitOption=SetOption("Explicit","Off") ;code generated by KiXforms designer $OldExplicitOption=SetOption("Explicit",$OldExplicitOption)
But anyway, I think much in your code and the (re)use of $ depends if $ is global or local. If you do not Dim it, most likely, it will be global.
|
Top
|
|
|
|
#167392 - 2006-09-10 11:39 PM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Thanks for the answers, I had always thought that any variable called within a function was exclusive to that function.
But I was getting some strange outputs on one of my scripts and thought to ask here.
So the next question would be... If I ...
Code:
Function 1() Dim $ $ = 123 2() EndFunction
Function 2() Dim $ $ = 456 EndFunction
Is the $ in 1() preserved? As I am calling Dim again within the second Function effectivly making $ = NUL
|
Top
|
|
|
|
#167393 - 2006-09-10 11:44 PM
Re: disposable variables and functions
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
try this one: Code:
Function 1() Dim $ $ = 123 2() $ ? EndFunction
Function 2() Dim $ $ = 456 EndFunction
when you dim, you dim to the current scope and scope for UDF is the udf. the dimming does not affect anywhere else.
and witto:
Quote:
If you do not Dim it, most likely, it will be global.
no, not most likely. if you don't dim, it is global. always.
|
Top
|
|
|
|
#167394 - 2006-09-11 12:02 AM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
So with that answer, I have a bunch of code to go through and fix...
I hate it when I get sloppy, some of it is done correctly some of it is not.
As a continuation however.... Code:
Function 1() Global $1[2] $1 = 123,456,789 EndFunction
Function 2() Redim Preserve $1[3] $1[3] = 0 EndFunction
This would allow for $1 to be carried throughout the script, but does the Global carry throughtout? I.E. does the Redim cause it to be local only, or will it remain Global.
If it does become local, is there a way to Redim Preserve a global?
|
Top
|
|
|
|
#167396 - 2006-09-11 12:18 AM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Quote:
setting a var with the name of udf is "illegal"
This is not really the issue, I just made that up as I typed. You could easily change it to... Code:
Function A() Global $1[2] $1 = 123,456,789 EndFunction
Function B() Redim Preserve $1[3] $1[3] = 0 EndFunction
|
Top
|
|
|
|
#167398 - 2006-09-11 12:29 AM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
But on another point...
I have read many times that one should not(does not need to) dim variables for those that are passed into a function EX: Function A($Input)
Why? Are they automatically created when you call the UDF and if so what are they considered global / local?
I have tested this with
Code:
Function DoOpen(Optional $OpenFile)
Dim $NumArray[55],$PnumArray[42],$Array[], $LBL_LNum[5], $LBL_HNum[5], $LBL_LastNum[6],$LastDraw[],$BTN_Details[5] Dim $OF,$FH,$,$Val Global $LBL_RNUM[5,6],$Count
If $OpenFile = "" $OF = $system.OpenFileDialog() $OF.DefaultExt = "txt" $OF.Filter = "Saved Prefs(*.txt)|*.txt" $OF.InitialDirectory = @scriptdir $OF.Title = "Save Preferences" $OF.AddExtension = "True" $OF.RestoreDirectory = "True" $OF.Showdialog $OPenFile = $OF.Filename ; Returning to console EndIf
As you can see I have not set the $OpenFile, yet with Explecit ON, it does not error, even though I am not passing that variable to the function.
Edited by Gargoyle (2006-09-11 12:30 AM)
|
Top
|
|
|
|
#167399 - 2006-09-11 12:32 AM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Quote:
in some version (the one where I last tried it) redimming a global was not possible.
Did you get an error? It seems to be working, but may be causing problems that I have not seen as of yet.
|
Top
|
|
|
|
#167400 - 2006-09-11 12:34 AM
Re: disposable variables and functions
|
Lonkero
KiX Master Guru
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
think there is no other naming to the parameters. they are semi-globals or better yet function-wide-globals. they are not set outside the udf but are automatically recognised inside the udf itself, as if they were globals.
I bet your next question is why the $DoOpen does not need to be dimmed either...
|
Top
|
|
|
|
#167401 - 2006-09-11 12:44 AM
Re: disposable variables and functions
|
Gargoyle
MM club member
Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
|
Actually once I put in the Explicit ON, I did get this...
Quote:
ERROR : duplicate definition of variable [output]!
When it came across me doing a second Global on the $output... Back to massive recoding....
And my assumption would be... $DoOpen is defined as Global by the use of the Keyword "Function"
|
Top
|
|
|
|
#167403 - 2006-09-13 09:18 AM
Re: disposable variables and functions
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
IMHO, $DoOpen is a local variable dimensioned in the function DoOpen (by the use of the Keyword "Function").
Yup, function return variables and parameters are local in scope.
Quick example: Code:
$=SetOption("Explicit","ON") Global $Fun $Fun="Foo" "Before function : '"+$Fun+"'"+@CRLF $=Fun() "After function : '"+$Fun+"'"+@CRLF Function Fun() "Start of function: '"+$Fun+"'"+@CRLF $Fun="Bar" "End of function : '"+$Fun+"'"+@CRLF EndFunction
Outputs as expected:
Quote:
Before function : 'Foo' Start of function: '' End of function : 'Bar' After function : 'Foo'
|
Top
|
|
|
|
#167404 - 2006-09-13 10:00 AM
Re: disposable variables and functions
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
I have tried (almost) everything that was discussed here. Maybe share the code I used. Just play a bit with semicolons here and there and watch the result when running the code. Code:
If NOT @LOGONMODE Break On EndIf Dim $SO $SO = SetOption("Explicit","On") $SO = SetOption("NoMacrosInStrings","On") $SO = SetOption("NoVarsInStrings","On") $SO = SetOption("WrapAtEOL","On")
Global $ReturnArray ;Dim $ReturnArray $ReturnArray = "NotAnArray" ? $ReturnArray ?
$SO = ReturnArray(5) Dim $Element For Each $Element In $SO ? $Element Next ?
? $ReturnArray ?
;; If $ReturnArray was global, this should work ;For Each $Element In $ReturnArray ; ? $Element ;Next
;; Test to see which is global ;? $TestLocal ;? $TestGlobal ? $TestGlobal[0] ? $TestGlobal[1] ?
? "KiX Executable: " + @SCRIPTEXE ? "KiX Version: " + @KIX ?
Function ReturnArray($i) Dim $TestLocal $TestLocal = "Local Variable" ;Global $TestGlobal ;$TestGlobal = "Global Variable" Global $TestGlobal[0] $TestGlobal[0] = "Global Variable First Value" ReDim Preserve $TestGlobal[1] $TestGlobal[1] = "Global Variable Second Value" ;? $TestGlobal[0] ;? $TestGlobal[1] Dim $j For $j = 0 to $i step 1 ReDim preserve $ReturnArray[$j] $ReturnArray[$j] = $j ;? $ReturnArray[$i] Next ;? EndFunction
;Names of Functions should not start with a number
;? 2 ;? 2b
;Function 2() ; ;Dummy ;EndFunction
;Function 2b() ; ;Dummy ;EndFunction
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 382 anonymous users online.
|
|
|