473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to have optional parameters with default values

wyo
I have a function with several parameter, each optional.

function test (a, b, c, d) {...}

How do I check if e.g. parameter "a" is missing and set it a default
value?
How can I use it with parameter "a" optional but "b", "c", "d"
specified?

O. Wyss

Apr 27 '07 #1
5 12857
On Apr 27, 7:12 am, wyo <otto.w...@orpatec.chwrote:
I have a function with several parameter, each optional.

function test (a, b, c, d) {...}

How do I check if e.g. parameter "a" is missing and set it a default
value?
if(a===undefined)a='default value'
How can I use it with parameter "a" optional but "b", "c", "d"
specified?
if(b===undefined)doSomething()
Apr 27 '07 #2
wyo
On Apr 27, 3:35 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On Apr 27, 7:12 am, wyo <otto.w...@orpatec.chwrote:

How can I use it with parameter "a" optional but "b", "c", "d"
specified?

if(b===undefined)doSomething()
and calling it as

test (,value_b, value_c, value_d);

Just leave off parameter "a"?

O. Wyss

Apr 27 '07 #3

wyo wrote:
I have a function with several parameter, each optional.

function test (a, b, c, d) {...}

How do I check if e.g. parameter "a" is missing and set it a default
value?
The first parameter passed to the function will be assigned to a, the
second to b, etc. If a is missing, then so are b, c and d.

function test (a, b, c, d, a) {
a = a || 'a';
b = b || 'b';
...
}

How can I use it with parameter "a" optional but "b", "c", "d"
specified?
function test (b, c, d, a) {...}

Parameters are passed by order, not name.
--
Rob

Apr 27 '07 #4
wyo
On 27 Apr., 16:23, RobG <r...@iinet.net.auwrote:
Parameters are passed by order, not name.
That means I've to give a dummy value like -1 for the first
parameter.

Thanks
O. Wyss

Apr 27 '07 #5

wyo wrote:
On 27 Apr., 16:23, RobG <r...@iinet.net.auwrote:
Parameters are passed by order, not name.
That means I've to give a dummy value like -1 for the first
parameter.
yes e.g. 0, empty string, null, undefined etc..

Apr 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rob Long | last post by:
Hey, I've just noticed a somewhat annoying feature with PHP5 type hinting. You cannot hint types on optional arguments like this: class MyClass { function someFunc(Type1 $arg1, Type2 $arg2...
2
by: Frank Rizzo | last post by:
Does c# support optional parameters in function declarations? Does it support assignment of default values to arguments, in particular optional parameters? If so, can someone point me to or...
21
by: Marc DVer | last post by:
I am trying to create a query that can be loaded as a querydef object but not having to assign values to the parameters if I don't want to. Normally when using a parameter query in VBA my code...
5
by: Imran Aziz | last post by:
Hello All, I am new to C# , how can I delare parameters to a function as optional, and also how are default values assigned ? consider the function public String myFunc(String thisisOptional,...
3
by: ruca | last post by:
Hi, How can I set a parameter of a function optinal and have a default value? In VB I do like this: Function myFunction (Optional ByVal bExist as Boolean = False) What's the equivalent in...
5
by: Water Cooler v2 | last post by:
So you have optional parameters for functions/methods in Visual Basic ..NET, but not in C#?
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
3
by: GS | last post by:
I have this: Public Function CleanHtml(ByVal strHtml As String, Optional ByVal doc As HtmlDocument = WebBrowser1.Document) As String flagged by vb as Error 5 Reference to a non-shared member...
9
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
I'll ask, how do you do it? -- (i''ll be asking a lot of these, but I find C# totally way cooler than vb and there''s no go''n back!!!) thanks (as always) kes
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.