Jump to content

can I make a user function with optional


adamgal
 Share

Recommended Posts

  • Replies 41
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

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

$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)
endfunction

Edit:

Last line of code was cut off.

Edited by SlimShady
Link to comment
Share on other sites

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

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

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.

Link to comment
Share on other sites

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 by this-is-me
Who else would I be?
Link to comment
Share on other sites

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

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 Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

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

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.

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