Jump to content

function default values


Recommended Posts

Is it possible to use later parameters in a function while using the default values earlier, for example the following code prints "Default"

 

_test(Default, 2)
Func _test($sString="Test", $iNum=1)
    ConsoleWrite($sString & @CRLF)
EndFunc

I know I can do

_test(Default, 2)
Func _test($sString="Test", $iNum=1)
   if $sString = Default then $sString="Test"
    ConsoleWrite($sString & @CRLF)
EndFunc

but it makes the code a lot more of a pain when you change values and you have to do this for each function etc, is it possible to have it so that $sString uses the default value without checking for "Default" and resetting it?

Edited by botanic
Link to comment
Share on other sites

26 minutes ago, botanic said:

but it makes the code a lot more of a pain when you change values and you have to do this for each function etc, is it possible to have it so that $sString uses the default value without checking for "Default" and resetting it?

No.  You have to manage that yourself.  And calling it a "pain" is a bit exaggerated, try programming it in ASM.  You will see what pain looks like...

Link to comment
Share on other sites

I read this post this morning and I never tried to override the value sent to a function before. I was puzzled why your attempt to override the sent value didn't work. Well, tonight I was working on a project totally separate from yours and discovered why what you are attempting did not work (just coincidence). Maybe my newfound knowledge can help.

Check out this piece of code:

$data1 = _FuncTest()
$data2 = _FuncTest("#2", "This Is Something Different")

Func _FuncTest($value1 = "#1", $value2 = "The Default Thing")  
    MsgBox(0, "", $value1 & " " & $value2)
Endfunc

For $data1, nothing is sent, therefore the function assigns default values because nothing has been sent.

For $data2, data has been sent, so that is what should be used.

In creating AutoIt, I assume the creators had to make a choice, always use the values sent by calling the function or always override incoming data for what the user specifies when the function is actually called. If you don't have default values for incoming data and the number of parameters don't match (some or all parameters were not sent), the function crashes and burns.

I hope this helps to understand the concept.

BTW, IMO AutoIt is the easiest programming language to understand I have ever seen. Others should be taking notes.

 

Cheers!

Link to comment
Share on other sites

2 hours ago, abberration said:

I assume the creators had to make a choice, always use the values sent by calling the function or always override incoming data for what the user specifies when the function is actually called. If you don't have default values for incoming data and the number of parameters don't match (some or all parameters were not sent), the function crashes and burns.

Not always one or the other.

(simplified and incomplete description ;))

  1. Functions can be defined with or without parameters
  2. there are non-optional parameters and optional parameters (see description of functions in the help)
  3. non-optional parameters must always be declared before optional parameters (if present)
  4. if e.g. an optional parameter2 is passed, then the optional parameter1 has to be specified as well (see e.g. keyword 'Default')

Example :

; -> _FuncTest() would fail, because the first parameter is non-optional !
; -> parameter 2,3,4 are optional
_FuncTest("non-optional param1 = 1")
_FuncTest("non-optional param1 = 2", "#2")
_FuncTest("non-optional param1 = 3", "#3", "Text New")
_FuncTest("non-optional param1 = 4", "#4", Default, 200)
_FuncTest("non-optional param1 = 5", "#5", Default, 500)

Func _FuncTest($sParam1, $sParam2 = "#1", $sParam3 = "Text Default", $iParam4 = 100)

    If $sParam3 = Default Then $sParam3 = "Text Default"

    MsgBox(BitOR(4096, 48), "Values :", _
                            $sParam1 & @CRLF & _
                            $sParam2 & @CRLF & _
                            $sParam3 & @CRLF & _
                            $iParam4 & @CRLF)

    ; change Param4 if value is 500 :
    If $iParam4 = 500 Then
        $iParam4 = 1000
        MsgBox(BitOR(4096, 48), "Param4 changed :", "New value = " & $iParam4 & @CRLF)
    EndIf
EndFunc

 

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

This is the result of a design choice: AutoIt doesn't currently support named, indexed or positional arguments.

It's easy to imagine an example syntax mixing all techniques: say that !name!= introduces a named argument, :3:= prefixes the third argument and commas permit  optional arguments, then all this is possible.

; optionally named arguments
; will print
; >12345<   >Hello< >World!<
f(!third!="World!", :!First!=12345)

Func f($first=0, $second="Hello", $third)
    ConsoleWrite(">" & $firdt & "<" & @TAB & ">" & $second & "<" & @TAB & ">" & $third & "<" & @LF)
EndFunc

; optionally indexed arguments
; will print
; >12345<   >Hello< >World!<
g(:3:="World!", :1:=12345)

Func g($arg1=0, $arg2="Hello", $arg3)
    ConsoleWrite(">" & $arg1 & "<" & @TAB & ">" & $arg2 & "<" & @TAB & ">" & $arg3 & "<" & @LF)
EndFunc

; positional arguments
; will print
; >12345<   >Hello< >World!<
h(12345, , "World!")

Func h($arg1=0, $arg2="Hello", $arg3)
    ConsoleWrite(">" & $arg1 & "<" & @TAB & ">" & $arg2 & "<" & @TAB & ">" & $arg3 & "<" & @LF)
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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