473,392 Members | 1,457 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,392 software developers and data experts.

function with optional parameter

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
7 36528
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
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
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


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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adam Dyga | last post by:
Hi, How to create function with optional parameter passed by reference? I've tried sth like this: function (&$param=NULL) // or: function (&$param=new array()) { //... }
1
by: Ole S. Pedersen | last post by:
I have two tables: eg. a person-table (no nulls allowed), with an id and so on, and a person_course table (an intermediate table in a many-to many relationship between person table and courses...
7
by: (-: Dan :-) | last post by:
hi everybody suppose I have a function in a DLL function F1(var1, var2, var3) where var2 and var3 are optional parameter with default value (for exsample "-1") I create a new instance of...
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...
10
by: John Morgan | last post by:
Does anyone know what parameter should be used instead of Date = 0 for the optional parameter in the following function? Public Function dhAge(ByVal dtmBD As Date, Optional ByVal dtmDate As Date...
1
by: Ed Chiu | last post by:
Hi, Is there a way to assign a DBNull value to an optional parameter of a function? I tried: Public Function AddUpdateCases( _ ByVal CrisCaseID As Integer, _ Optional ByVal APN As DateTime...
1
by: KayC | last post by:
Hi I am using Access2002 I have a form with 5 text boxes which are parameters to a function 3 parameters are optional in the function How do I pass the empty textbox values to a optional...
2
by: Curious | last post by:
I have two methods that are almost identical, except for the "HtmlLogger varLogger" parameter passed to #2. In #1, it uses a default HtmlLogger "logger" while in #2, it uses "varLogger" parameter....
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.