function with optional parameter

J

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

Ray at

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
 
W

William Morris

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
 
H

Harag

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;
}
 
C

Chris Hohmann

Harag said:
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
 
H

Harag

On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"

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

Chris Hohmann

Harag said:
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"

[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top