Jump to content

Function parameter not being recognised


Recommended Posts

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.

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Link to comment
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 ...
 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...