Page 1 of 1 1
Topic Options
#167387 - 2006-09-10 06:56 PM disposable variables and functions
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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"
_________________________
Today is the tomorrow you worried about yesterday.

Top
#167388 - 2006-09-10 08:12 PM Re: disposable variables and functions
Witto Offline
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 Offline
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
#167390 - 2006-09-10 09:57 PM Re: disposable variables and functions
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
#167391 - 2006-09-10 10:28 PM Re: disposable variables and functions
Witto Offline
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 Offline
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 Administrator Offline
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 Offline
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
#167395 - 2006-09-11 12:08 AM Re: disposable variables and functions
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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...

Top
#167396 - 2006-09-11 12:18 AM Re: disposable variables and functions
Gargoyle Offline
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
#167397 - 2006-09-11 12:26 AM Re: disposable variables and functions
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
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.

Top
#167398 - 2006-09-11 12:29 AM Re: disposable variables and functions
Gargoyle Offline
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 Offline
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 Administrator Offline
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 Offline
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
#167402 - 2006-09-11 03:55 PM Re: disposable variables and functions
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
IMHO, $DoOpen is a local variable dimensioned in the function DoOpen (by the use of the Keyword "Function").
Top
#167403 - 2006-09-13 09:18 AM Re: disposable variables and functions
Richard H. Administrator Offline
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 Offline
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
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.076 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org