Sign in to follow this  
Followers 0
SublimePorte

Function parameter not being recognised

10 posts in this topic

I have a function with a default parameter, which for some reason is not being recognised.

Is there anything (I assume in my function) that could be causing it to give a "variable used before being declared error"?

Func MyFunction($auto=0)
    If $auto Then Msgbox(0, "Test", "$auto = " & $auto)
Endfunc

When I put this in a script by itself, it works fine (as it should), yet inside my script it says $auto has not been declared.

Share this post


Link to post
Share on other sites



Works fine for me v3.3.6.1

AutoItSetOption('MustDeclareVars',1)

MyFunction('1') ;msgbox
MyFunction('0') ;msgbox
MyFunction()
MyFunction('j') ;msgbox

Func MyFunction($auto=0)
    If $auto Then Msgbox(0, "Test", "$auto = " & $auto)
Endfunc

Share this post


Link to post
Share on other sites

I've discovered the reason why.

It's being called from somewhere else via SetOnGuiEvent, that's why it's not taking the parameter.

Shouldn't it still take the default value though of 0??

Share this post


Link to post
Share on other sites

If you are trying to access the value of $auto outside the scope of MyFunction you need to use Global $auto = 0 and define no parameters. You could also still use the Local/Dim scope but pass by reference (func MyFunction(ByRef $auto)) but it depends how you need to use $auto.

Share this post


Link to post
Share on other sites

It is actually like this:

Func MyFunction($auto=0)
    If $auto Then Msgbox(0, "Test", "$auto = " & $auto)
Endfunc

GUICtrlCreateButton("Clickme", 10, 10)
GUICtrlSetOnEvent(-1, "MyFunction")

But I still can't see why the default value of $auto can't be shown in the msgbox inside the function.

Share this post


Link to post
Share on other sites

Hi,

Because GUICtrlSetOnEvent() can not call a function that uses Params.

If you need GUICtrlSetOnEvent() to call a function that uses Param(s) then point GUICtrlSetOnEvent() to an Event Handler function that can call the MyFunction() passing a param.

Opt("GUIOnEventMode", 1)

GUICreate("")
$Button1 = GUICtrlCreateButton("Clickme", 10, 10)
GUICtrlSetOnEvent(-1, "MyEvent")
GUISetOnEvent(-3, "MyEvent")
GUISetState()

While 1
    Sleep(10)
WEnd

Func MyEvent()
    Switch @GUI_CtrlId
        Case $Button1
            MyFunction("$Button1 CtrlID: " & @GUI_CtrlId)
        Case -3
            MyFunction("$GUI_EVENT_CLOSE CtrlID: " & @GUI_CtrlId)
            Exit
    EndSwitch
EndFunc   ;==>MyEvent

Func MyFunction($auto = 0)
    If $auto Then MsgBox(0, "Test", "$auto = " & $auto)
EndFunc   ;==>MyFunction

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Because GUICtrlSetOnEvent() can not call a function that uses Params.

Sure it can. OP proofed that. Problem is ...

I've discovered the reason why.

It's being called from somewhere else via SetOnGuiEvent, that's why it's not taking the parameter.

Shouldn't it still take the default value though of 0??

Nope. The parameter part of a function thats called from something like a SetOnGuiEvent() will not have its parameter part parsed. (or the function behaves like it has no parameters.)

A alternative could be by dropping something like "If Not IsDeclared ("auto") then Assign("auto", 0, 0)" in MyFunction2.

(personally I generally also use the function linking as workaround, as IsDeclared and Assign could trigger other potential problems.)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Sure it can. OP proofed that. Problem is ...

Well obviously it can be called, since that was the source of my problem. What I meant was it can't be called normally (ie. with default parameters being passed).

A alternative could be by dropping something like "If Not IsDeclared ("auto") then Assign("auto", 0, 0)" in MyFunction2.

That's not a bad idea, but I personally don't like the idea of doing that, as you said it could lead to potential problems, that might waste time trying to hunt down.

The solution I have posted above works fine.

Edited by SublimePorte

Share this post


Link to post
Share on other sites

Sure it can. OP proofed that. Problem is ...

What I meant was ...

Your commenting on a comment I made on someone else his/her (technically incorrect) comment ?

Anyway. Others with less knowledge might interpreted it as its written, and learn something thats incorrect.

The solution I have posted above works fine.

I never said, nor implied, that it did not. Nor did I imply that the other one was better.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0