Gargoyle
(MM club member)
2006-09-10 06:56 PM
disposable variables and functions

Okay maybe a stupid question here but....

I am using $ as "disposable variable within a function, if in that function I call function2 with the variable $ used in it, are they exculsive to that function?

Code:

Function1()
$ = T
Function2()
EndFunction

Function2
$= Z
EndFunction



Will $ remain "T" in Function1 and "Z" in Function2, or does Function1 $ now equal "Z"


Witto
(MM club member)
2006-09-10 08:12 PM
Re: disposable variables and functions

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


Gargoyle
(MM club member)
2006-09-10 08:27 PM
Re: disposable variables and functions

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


Sealeopard
(KiX Master)
2006-09-10 09:57 PM
Re: disposable variables and functions

Variable scoping is explained in the KiXtart Manual. Not using DIM/GLOBAL is being frowned upon, SETOPTION('Explicit','ON') is recommended in order to prevent incorrect variable scoping or typos in variables. Generally, use DIM, unless GLOBAL is absolutely required. I, for example, use GLOBAL only to define KiXforms objects so that they are accessible even from within functions.

Witto
(MM club member)
2006-09-10 10:28 PM
Re: disposable variables and functions

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.


Gargoyle
(MM club member)
2006-09-10 11:39 PM
Re: disposable variables and functions

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


LonkeroAdministrator
(KiX Master Guru)
2006-09-10 11:44 PM
Re: disposable variables and functions

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.


Gargoyle
(MM club member)
2006-09-11 12:02 AM
Re: disposable variables and functions

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?


LonkeroAdministrator
(KiX Master Guru)
2006-09-11 12:08 AM
Re: disposable variables and functions

hmm...
now you are on shaky ground.
setting a var with the name of udf is "illegal"
it does work in some ways occassionally but there is no prove it will work in another version of kix, etc...


Gargoyle
(MM club member)
2006-09-11 12:18 AM
Re: disposable variables and functions

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



LonkeroAdministrator
(KiX Master Guru)
2006-09-11 12:26 AM
Re: disposable variables and functions

ok, to answer to the question, global remains global, no matter what.
if you can redim a global, then it remains a global.
iirc, in some version (the one where I last tried it) redimming a global was not possible.


Gargoyle
(MM club member)
2006-09-11 12:29 AM
Re: disposable variables and functions

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.


Gargoyle
(MM club member)
2006-09-11 12:32 AM
Re: disposable variables and functions

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.


LonkeroAdministrator
(KiX Master Guru)
2006-09-11 12:34 AM
Re: disposable variables and functions

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...


Gargoyle
(MM club member)
2006-09-11 12:44 AM
Re: disposable variables and functions

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"


Witto
(MM club member)
2006-09-11 03:55 PM
Re: disposable variables and functions

IMHO, $DoOpen is a local variable dimensioned in the function DoOpen (by the use of the Keyword "Function").

Richard H.Administrator
(KiX Supporter)
2006-09-13 09:18 AM
Re: disposable variables and functions

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'




Witto
(MM club member)
2006-09-13 10:00 AM
Re: disposable variables and functions

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