Jump to content

How to setup function to accept variable input?


Recommended Posts

In the help file it shows an example that is like this:

Func max($x, $y)  ;Return the larger of two numbers
    If $x > $y Then
        return $x
    Else
        return $y
    EndIf
EndFunc

And then you call that function like so

max(5, 6)

I am trying to pass ONE variable to a function by doing this:

Func myFunc($pass)
 ; there would be more code here
Return
EndFunc

Now when I try to call that function like so:

myFunc(55) ; This does not work

myFunc('pass') ; This does not work

myFunc("pass") ; This does not work

When I compile I get the following errors

D:\Projects\myTest.au3(916,15) : ERROR: myFunc() called with wrong number of args.

myFunc()

~~~~~~~~~~~~^

D:\Projects\myTest.au3(420,24) : REF: definition of myFunc().

Func myFunc($pass)

What am I doing wrong?

in php I can do this:

function myFunc($pass)  // when declaring function

OR


function myFunc($pass = '55') // when declaring function with a default value for passed variable if nothing passed


THEN I call the function like so:

myFunc("something here")

How can I do this with AutoIT v3?

I am using the latest stable version from the site.

Thank you for any help you can offer.

Link to comment
Share on other sites

This works

myFunc(55)

Func myFunc($pass)

; there would be more code here

MsgBox(0,"",$pass)

EndFunc

REB

Yeah, I deleted the function and saved the file. Then I recreated it and it worked. I don't know why it didn't before but for those that come to this thread everything in my OP does work and that is how you pass variables to functions in AutoIT.

Thanks

Link to comment
Share on other sites

in php I can do this:

function myFunc($pass = '55') // when declaring function with a default value for passed variable if nothing passed

AutoIt function parameters can be set in the same way so that if you called the function with no parameters, $pass would equal 55 in this example.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

AutoIt function parameters can be set in the same way so that if you called the function with no parameters, $pass would equal 55 in this example.

Thank you, I did not know that. It isn't shown in the examples in the help at all but it is good to know that it works that way. I will try it out, thanks.

Link to comment
Share on other sites

Thank you, I did not know that. It isn't shown in the examples in the help at all but it is good to know that it works that way. I will try it out, thanks.

It's on the same page that you took max() from in helpfile! (Func...Return...EndFunc)

Not only that, it's both in that yellow-definition-thingy, the remarks section, AND in the example!

:unsure:

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