Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > function with optional parameter

Reply
Thread Tools

function with optional parameter

 
 
JT
Guest
Posts: n/a
 
      01-14-2004
how can i declare a function that will accept an optional parameter?

something like:

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

thanks much.


 
Reply With Quote
 
 
 
 
Bob Barrows
Guest
Posts: n/a
 
      01-14-2004
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.


 
Reply With Quote
 
 
 
 
Ray at
Guest
Posts: n/a
 
      01-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> how can i declare a function that will accept an optional parameter?
>
> something like:
>
> function newFunc(strValue1, strValue2) --where strValue2 is optional.
>
> thanks much.
>
>



 
Reply With Quote
 
William Morris
Guest
Posts: n/a
 
      01-14-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> how can i declare a function that will accept an optional parameter?
>
> something like:
>
> function newFunc(strValue1, strValue2) --where strValue2 is optional.
>
> thanks much.
>
>



 
Reply With Quote
 
Harag
Guest
Posts: n/a
 
      01-14-2004


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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> how can i declare a function that will accept an optional parameter?
>>
>> something like:
>>
>> function newFunc(strValue1, strValue2) --where strValue2 is optional.
>>
>> thanks much.
>>
>>

>


 
Reply With Quote
 
Chris Hohmann
Guest
Posts: n/a
 
      01-14-2004
"Harag" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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


 
Reply With Quote
 
Harag
Guest
Posts: n/a
 
      01-15-2004
On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Chris Hohmann
Guest
Posts: n/a
 
      01-15-2004
"Harag" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
> <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining a method with an optional parameter Ahmed Moustafa Java 6 04-17-2012 12:34 PM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
Bug in python!? persistent value of an optional parameter in function! C Barr Leigh Python 12 03-08-2007 08:12 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
calling a optional parameter function (-: Dan :-) Javascript 7 10-13-2003 11:56 AM



Advertisments