adamgal 0 Posted November 13, 2004 I was wondering if there's a way to make a user function with optional params like the built in ones thank you Share this post Link to post Share on other sites
SlimShady 1 Posted November 13, 2004 Can't you add it? I don't have a need for it now but it would be nice if it's not difficult to add. Share this post Link to post Share on other sites
this-is-me 6 Posted November 14, 2004 For the record, I am repeating my personal vot for this as well. I am almost certain Larry cannot do it himself, but maybe Jon or Valik can. I have seen many users request this functionality. Who else would I be? Share this post Link to post Share on other sites
Holger 14 Posted November 14, 2004 @this-is-me: what especially do you mean? You can right now use parameters in functions...I think you mean something other, don't you? Holger Old project:GUI/Tray menu with icons and colorsOther old stuff:IconFileScanner, TriState/ThreeState GUI TreeView, GUI ContextMenu created out of a TreeView Share this post Link to post Share on other sites
SlimShady 1 Posted November 14, 2004 (edited) @Holger- Read the first post- I'll see if I can make an example...I can't. Wait I found a KixTart script the other day that uses it.The qsort function has optional parameters.expandcollapse popup$array = Peach,Pumpkin,Orange,Grape,Lime,Apple,Rasberry,Cherry,Lemon $array = qsort($array); sort ascending for each $element in $array ? $element next $array = 1,2,3,4,5 $array = qsort($array,1); sort descending for each $element in $array ? $element next $array = qsort(split("Z Q G A D M U V N K I X W J T S C R H B L E F P O Y")) for each $letter in $array $letter next function qsort($array,optional $order, optional $left, optional $right) dim $temp,$last,$i,$j if vartype($left) = 0 $qsort=qsort($array,$order,0,ubound($array)) return endif if $left >= $right $qsort=$array return endif $temp=$array[$left] $j=($left+$right)/2 $array[$left]=$array[$j] $array[$j]=$temp $last=$left for $i=$left+1 to $right if ($array[$i] < $array[$left] and not $order) or ($array[$i] > $array[$left] and $order) $last=$last+1 $temp=$array[$last] $array[$last]=$array[$i] $array[$i]=$temp endif next $temp=$array[$left] $array[$left]=$array[$last] $array[$last]=$temp $qsort=qsort($array,$order,$left,$last-1) $qsort=qsort($qsort,$order,$last+1,$right) endfunctionEdit:Last line of code was cut off. Edited November 14, 2004 by SlimShady Share this post Link to post Share on other sites
Jon 1,009 Posted November 14, 2004 A lot of script relies on $right existing. How does it handle the fact that it is optional? Deployment Blog:Â https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming:Â https://www.autoitconsulting.com/site/sccm-sdk/ Share this post Link to post Share on other sites
SlimShady 1 Posted November 14, 2004 I don't know.Why don't you contact the developers?KiXtart is developed by Ruud van Velsen of Microsoft Netherlands. Copyright 2001. All Rights Reserved. The KiXtart Resource Center, located at www.scriptlogic.com is intended as a self-help portal for the international community of KiXtart users. For direct questions or feedback concerning the KiXtart utility, please contact KiXtart2001 at hotmail.com.He's active on this forum. Share this post Link to post Share on other sites
ezzetabi 3 Posted November 14, 2004 Why not using the Larry idea? I usually use @lf as separing char since it is difficult that Parameters have @lf inside. Share this post Link to post Share on other sites
this-is-me 6 Posted November 14, 2004 I would imagine that there would need to be a null variable or macro. Follow: func test($str, optional $num) if $num <> @null Then Return $str & $num Else Return $str EndIf EndFunc If someone tried to reference an optional variable that didn't exist, then the value would be @null. Who else would I be? Share this post Link to post Share on other sites
scriptkitty 1 Posted November 15, 2004 I would like optional, but I use a variation of Larry's code. Only problem is when I pass an array to a UDF. So it would be nice for some scripts. I cna live with it though. What I would like is to be able to copy and paste Binary. It would be nice to have BinGet() and BinPut(). It would come in handy when you need to temporarly store non text information in a variable and then give it back. Seems every time I say something like this that it is actually already in the unstable and I missed it. AutoIt3, the MACGYVER Pocket Knife for computers. Share this post Link to post Share on other sites
bcording 0 Posted November 15, 2004 I would imagine that there would need to be a null variable or macro.I second the idea of a NULL variable or macro. It will be needed for the future if a binary file read/write method is implimented. Share this post Link to post Share on other sites
this-is-me 6 Posted November 15, 2004 (edited) One of the main reasons I see for needing a set of optional parameters to a udf is when the UDF needs a large amount of parameters, but the code calling it needs not specify all in all circumstances.A good example is a gui msgbox replacement.Current Code:Func MsgBox2($settings, $title, $message, $buttonset, $soundtoplay, $iconfile, $iconindex, $left, $top, $width, $height) ;;;code here EndFunc MsgBox2(64, "Testing", "This is a test of msgbox2", "", "", "", "", "", "", "", "")Possible future implementation (since all optional parameters would have to be at the end anyway)Func MsgBox2($settings, $title, $message, optional[$buttonset, $soundtoplay, $iconfile, $iconindex, $left, $top, $width, $height]) ;;;code here EndFunc MsgBox2(64, "Testing", "This is a test of msgbox2")This is one of the only reasons I have for requesting these features. However, if the syntax for array declaration could be completed (more here) then you could also do the following:Func MsgBox2($array) ;;;code here EndFunc MsgBox2([64, "Testing", "This is a test of msgbox2"])or, looking at the code on that page, maybe this:Func MsgBox2($array) ;;;code here EndFunc $test[] = [64, "Testing", "This is a test of msgbox2"] MsgBox2($test)and then parse the array items inside the function. Edited November 15, 2004 by this-is-me Who else would I be? Share this post Link to post Share on other sites
SlimShady 1 Posted November 15, 2004 (edited) @this-is-me The way you added "Optional" may cause problems because it looks like a function. I would prefer this: Func MsgBox2($settings, $title, $message, optional $buttonset, optional $soundtoplay, optional $iconfile, optional $iconindex, optional $left, optional $top, optional $width, optional $height) ;;;code here EndFunc MsgBox2(64, "Testing", "This is a test of msgbox2") That array definition you made is nice. Maybe we can do it this way with multiple dimensions: ;3 Dimensions Dim $test[][4][2] $test[0,0] = [64, "Testing", "This is a test of msgbox2"] $test[1,0] = [64, "Testing", "This is a test of msgbox2"] $test[2,0] = [64, "Testing", "This is a test of msgbox2"] $test[3,0] = [64, "Testing", "This is a test of msgbox2"] $test[0,1] = [64, "Testing", "This is a test of msgbox2"] $test[1,1] = [64, "Testing", "This is a test of msgbox2"] $test[2,1] = [64, "Testing", "This is a test of msgbox2"] $test[3,1] = [64, "Testing", "This is a test of msgbox2"] ;2 Dimensions Dim $test[][4] $test[0] = [64, "Testing", "This is a test of msgbox2"] $test[1] = [64, "Testing", "This is a test of msgbox2"] $test[2] = [64, "Testing", "This is a test of msgbox2"] $test[3] = [64, "Testing", "This is a test of msgbox2"] ;1 Dimension Dim $test[] $test[] = [64, "Testing", "This is a test of msgbox2"] Maybe instead of just Dim $test[], make the number necessary: Dim $test[3]. Edited November 15, 2004 by SlimShady Share this post Link to post Share on other sites
Nutster 3 Posted November 15, 2004 I would prefer using something similar to C++'s optional system. Func MyFunc($required, $needed, $opt1 = 10, $opt2 = "Default") This way, a default value is assigned if the argument is not given. I know I do not have the time to work on this as well. I think this should go on the TO DO list. David NuttallNuttall Computer ConsultingAn Aquarius born during the Age of AquariusAutoIt allows me to re-invent the wheel so much faster.I'm off to write a wizard, a wonderful wizard of odd... Share this post Link to post Share on other sites
this-is-me 6 Posted November 15, 2004 That would absolutely make sense. If a value was not needed, then a value would still be able to be read from the UDF. Hey, Jon, I think we have all come to a conclusion that can be fairly easily done. What about it? Who else would I be? Share this post Link to post Share on other sites
jpm 93 Posted November 15, 2004 Do I need to extract from the old dbg-bastard the code I wrote last year? Func...EndFunc -------------------------------------------------------------------------------- Defines a user-defined function that takes zero or more arguments and optionally returns a result. Func functioname ( [byRef] $param1, ..., [,Optional [byRef] $paramN+1, ... ]) ... [Return value] EndFunc Parameters The parameters are set by you. You later call the function like any other built-in function. Remarks The ByRef keyword is optional, and indicates that a value should be passed by reference so that a function may change a variable from another function. Optional parameters can be defined after the "Optional" keyword. The number of parameter that have been passed to a user function can be checked by using the @NumParams macros. Related See also: Dim/Global/Local, #include ,@NumParams Examples ; Sample script#1 with three user-defined functions $foo = 2 $bar = 5 msgBox(0,"Today is " & today(), "Let's make $foo = 3") swap($foo, $bar) msgBox(0,"After swapping $foo and $bar", "$foo now contains " & $foo) msgBox(0,"Finally", "The larger of 3 and 4 is " & max(3,4)) Exit Func swap(ByRef $a, ByRef $ ;swap the contents of two variables Dim $t $t = $a $a = $b $b = $t EndFunc Func today() ;Return the current date in mm/dd/yyyy form return (@MON & "/" & @MDAY & "/" & @YEAR) EndFunc Func max($x, $y) ;Return the larger of two numbers If $x > $y Then return $x Else return $y EndIf EndFunc ;End of sample script ; ---------- Sample script#2 with two optional parameters Func functioname ($param1, $param2, Optional $param3, $param4) ;... if @NumParams = 3 then ;... ; specific statements if the function is called with 3 parameters EndIf ;... Return 2 EndFunc ;End of sample script Share this post Link to post Share on other sites
this-is-me 6 Posted November 15, 2004 ??? Are you saying that the code you have shown is the way you would like it to be ??? Who else would I be? Share this post Link to post Share on other sites
Valik 478 Posted November 15, 2004 JP, your code may be a good base, but I agree with David, it should be like C++; that method covers the most important things:No new keyword is necessary (Makes readability easier).The parameters can be initialized to something if necessary.this-is-me, we've all come to this conclusion before, maybe even a couple times, but its still not been added... Share this post Link to post Share on other sites
jpm 93 Posted November 16, 2004 (edited) JP, your code may be a good base, but I agree with David, it should be like C++; that method covers the most important things:No new keyword is necessary (Makes readability easier). The parameters can be initialized to something if necessary.this-is-me, we've all come to this conclusion before, maybe even a couple times, but its still not been added... <{POST_SNAPBACK}>I ask this kind of syntax with kind of keywording long time ago. To be franck I was afraid to introduce this kind of change in the call sequence. So the Optional keyword was a simple solution no more than 50 lines added to the code at the time I did. Edited November 16, 2004 by jpm Share this post Link to post Share on other sites
DaveF 0 Posted November 16, 2004 Have you tried using the #include functionality? Most of this could be solved in 10 seconds by coding a wrapper around the function / UDF that you don't need to configure all the parameters for, or that you expect to use the same arguments with each time: Func DebugMsgBox($bugMsg) MsgBox(64, "Debug", $bugMsg, 10) EndFunc With UDFs you can use this method with a C-language "OpenGL"-style naming convention to reflect the number of arguments you want to provide: Func TheFunc($target, $manner) While 1 If _Mangle($target, $manner) Then ExitLoop Wend EndFunc Func TheFunc1($target) While 1 If _Mangle($target, "gently") Then ExitLoop Wend EndFunc Func TheFunc0($target, $manner) While 1 If _Mangle("The world", "gently") Then ExitLoop Wend EndFunc Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines. Share this post Link to post Share on other sites