| View previous topic :: View next topic |
| Author |
Message |
Rhys
Joined: 17 Apr 2007 Posts: 710 Location: Florida
|
Posted: Thu Jun 05, 2008 3:49 am Post subject: [Solved] Blank Optional 1st Parameter in Function? |
|
|
I'm not sure if this is a bug or not, but the following code fails: | Code: | MsgBox,% Add() ;4 (3 + 1)
MsgBox,% Add(2) ;3 (2 + 1)
MsgBox,% Add(,2) ;Error but should be 5 (3 + 2)
Add(x=3, y=1)
{
return x + y
} | What am I doing wrong? Is it not possible to leave the first parameter of a function blank but provide other parameters? _________________ [Join IRC!]

Last edited by Rhys on Thu Jun 05, 2008 6:16 am; edited 1 time in total |
|
| Back to top |
|
 |
Ian
Joined: 15 Jul 2007 Posts: 1151 Location: Enterprise, Alabama
|
Posted: Thu Jun 05, 2008 4:08 am Post subject: |
|
|
It doesn't want you to omit parameters. _________________ ScriptPad/~dieom/dieom/izwian2k7/Trikster/God |
|
| Back to top |
|
 |
Rhys
Joined: 17 Apr 2007 Posts: 710 Location: Florida
|
Posted: Thu Jun 05, 2008 5:39 am Post subject: |
|
|
But I can omit parameters in the first two lines without issue. _________________ [Join IRC!]
 |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6772 Location: Pacific Northwest, US
|
Posted: Thu Jun 05, 2008 5:51 am Post subject: |
|
|
I think all of the omitted parameters must be last. I think this is the same reasoning that all optional parameters in the function definition must be last as well. If you could omit parameters that were followed by non-omitted parameters, then this would not be a requirement.
The closest relevant documentation is
| chm wrote: | | It is not possible to have optional parameters isolated in the middle of the parameter list. In other words, all parameters that lie to the right of the first optional parameter must also be marked optional. |
This could probably be explained a bit further, and may actually be elsewhere in the Functions page, but I did not see it on a quick glance. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
Rhys
Joined: 17 Apr 2007 Posts: 710 Location: Florida
|
Posted: Thu Jun 05, 2008 6:15 am Post subject: |
|
|
That makes sense, thanks! _________________ [Join IRC!]
 |
|
| Back to top |
|
 |
corrupt
Joined: 29 Dec 2004 Posts: 2393
|
Posted: Thu Jun 05, 2008 6:22 am Post subject: |
|
|
There are likely alternatives depending on what you'd like to accomplish  | Code: | MsgBox,% Add() ;4 (3 + 1)
MsgBox,% Add(2) ;3 (2 + 1)
MsgBox,% Add("",2)
MsgBox,% Add(myvar,2)
b = pickles
MsgBox,% Add(b,8)
Add(x="", y=1)
{
if x is not number
X=3
return x + y
} |
|
|
| Back to top |
|
 |
|