By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,365 Members | 3,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,365 IT Pros & Developers. It's quick & easy.

function with optional parameter

P: n/a
JT
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValue1, strValue2) --where strValue2 is optional.

thanks much.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
JT wrote:
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValue1, strValue2) --where strValue2 is optional.

thanks much.


http://www.4guysfromrolla.com/webtech/071801-1.shtml

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #2

P: n/a
What language? VBScript or JScript? Optional arguments are not supported
in VB Script, unfortunately. Here's a work around that someone has come up
with. http://www.4guysfromrolla.com/webtech/071801-1.shtml

I'm ~pretty~ sure that jscript does not support optional arguments either,
but hopefully someone can verify that, as I'm a jscript novice at best.

Ray at work

"JT" <je******@sppinc.net> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValue1, strValue2) --where strValue2 is optional.

thanks much.

Jul 19 '05 #3

P: n/a
Optional parameters aren't supported in VBScript. What we do in our code is
this:

Call MySub("sherlock|holmes|pipe|hat|coat")

Sub MySub(parms)
const cFirstname = 0
const cLastname = 1
const cAccessory = 2
const cClothing1 = 3
const cClothing2 = 4
parms = split(parms, "|")
redim preserve parms(cClothing2)
'--- set up defaults (if any)
if parms(cFirstname) = "" then parms(cFirstname) = "John"
if parms(cLastname) = "" then parms(cLastname) = "Watson"
'--- other processing with the values
End Sub

If you're looking strictly at true/false values you can do it this way:

Call MySub(12)

Sub MySub(parms)
const tfparm1 = 2
const tfparm2 = 4
const tfparm3 = 8
if (parms and tfParm1) = tfParm1 then
'--- some processing here
end if
if (parms and tfParm2) = tfParm2 then
'--- some processing here
end if
if (parms and tfParm3) = tfParm3 then
'--- some processing here
end if
End Sub

"JT" <je******@sppinc.net> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValue1, strValue2) --where strValue2 is optional.

thanks much.

Jul 19 '05 #4

P: n/a


Ray, from one novice to another :)

I think you can use optional arguments with Jscript. but you can't
skip a parameter... eg if you leave off arg3 below you also have to
leave off 4 & 5.

if there is a way of just skipping arg3 below then I dont know it and
maybe a guru will help :)

Also I've learned you can pass an object to a function (myfunc2 below)
which might help

HTH
Al.

function Myfunc (arg1, arg2, arg3, arg4, arg5) {

// the below will asign the value after the || if the arg1
// is "undefined/null"
var a1 = arg1 || ""; // undefined...set to ""
var a2 = arg2 || 0; // undefined...set to 0
var a3 = arg3 || 0; // undefined...set to 0
var a4 = arg4 || ""; // undefined...set to ""
var a5 = arg5 || 0; // undefined...set to 0

// or you can do this:

if (typeof arg1 == "undefined") {
arg1 = "";
}

//etc
}
// calling a func to pass an object.
// note the curly brackets INSIDE the rounded ones.
var RetVal = myFunc2 ( {x :10, y:20, z : 30, text:"myText: "} );

// function "requires" x & y properties... z & text is optional
function myFunc2 (obj) {
var calc = obj.x * obj.y;

if (typeof obj.z == "number") {
calc *= obj.z;
}
if (typeof obj.text == "string") {
return obj.text + calc
}
return calc;
}

On Wed, 14 Jan 2004 15:35:38 -0500, "Ray at <%=sLocation%>"
<myfirstname at lane34 dot com> wrote:
What language? VBScript or JScript? Optional arguments are not supported
in VB Script, unfortunately. Here's a work around that someone has come up
with. http://www.4guysfromrolla.com/webtech/071801-1.shtml

I'm ~pretty~ sure that jscript does not support optional arguments either,
but hopefully someone can verify that, as I'm a jscript novice at best.

Ray at work

"JT" <je******@sppinc.net> wrote in message
news:OL**************@TK2MSFTNGP11.phx.gbl...
how can i declare a function that will accept an optional parameter?

something like:

function newFunc(strValue1, strValue2) --where strValue2 is optional.

thanks much.


Jul 19 '05 #5

P: n/a
"Harag" <ha***@REMOVETHESECAPITALSsofthome.net> wrote in message
news:hj********************************@4ax.com...


Ray, from one novice to another :)

I think you can use optional arguments with Jscript. but you can't
skip a parameter... eg if you leave off arg3 below you also have to
leave off 4 & 5.

if there is a way of just skipping arg3 below then I dont know it and
maybe a guru will help :)


<script language="JavaScript" runat="SERVER">
function argTest(first,second,third){
Response.Write("<br>First: " + (first?first:"No parameter specified"));
Response.Write("<br>Second: " + (second?second:"No parameter
specified"));
Response.Write("<br>Third: " + (third?third:"No parameter specified"));
}
</script>

<script language="VBScript" runat="SERVER">
argTest "Apple",,"Cantaloupe"
</script>

-Chris Hohmann
Jul 19 '05 #6

P: n/a
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
<no****@thankyou.com> wrote:

[snipped]

[skipping an argument]
<script language="VBScript" runat="SERVER">
argTest "Apple",,"Cantaloupe"
</script>

-Chris Hohmann

ahh thanks Chris, didn't know you could just put double commas in.

I normally use either double quote (empty string) or put the word
null.

What I was tring to say is I dont know of a way where you can put the
parameters in whatever order you feel like it. I only know that you
have to specify them in order from first to last. Unless you pass it
as an object like the second function in the prev post.

By skipping I was thinking along the lines of parameters in a MSSQL
Stored Procedure.

EXEC usp_SampleProcedure @EmpID = 9, @EmpName = 'Jim'

Al.
Jul 19 '05 #7

P: n/a
"Harag" <ha***@REMOVETHESECAPITALSsofthome.net> wrote in message
news:ab********************************@4ax.com...
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
<no****@thankyou.com> wrote:

[snipped]

[skipping an argument]
<script language="VBScript" runat="SERVER">
argTest "Apple",,"Cantaloupe"
</script>

-Chris Hohmann

ahh thanks Chris, didn't know you could just put double commas in.

I normally use either double quote (empty string) or put the word
null.

What I was tring to say is I dont know of a way where you can put the
parameters in whatever order you feel like it. I only know that you
have to specify them in order from first to last. Unless you pass it
as an object like the second function in the prev post.

By skipping I was thinking along the lines of parameters in a MSSQL
Stored Procedure.

EXEC usp_SampleProcedure @EmpID = 9, @EmpName = 'Jim'

Al.


I think the term is "named parameters". As far as I know neither JScript
nor VBScript supports named parameters.

HTH
-Chris Hohmann
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.