function with optional parameter

Discussion in 'ASP General' started by JT, Jan 14, 2004.

  1. JT

    JT Guest

    how can i declare a function that will accept an optional parameter?

    something like:

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

    thanks much.
     
    JT, Jan 14, 2004
    #1
    1. Advertising

  2. JT

    Bob Barrows Guest

    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.
     
    Bob Barrows, Jan 14, 2004
    #2
    1. Advertising

  3. JT

    Ray at Guest

    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" <> wrote in message
    news:...
    > how can i declare a function that will accept an optional parameter?
    >
    > something like:
    >
    > function newFunc(strValue1, strValue2) --where strValue2 is optional.
    >
    > thanks much.
    >
    >
     
    Ray at, Jan 14, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > how can i declare a function that will accept an optional parameter?
    >
    > something like:
    >
    > function newFunc(strValue1, strValue2) --where strValue2 is optional.
    >
    > thanks much.
    >
    >
     
    William Morris, Jan 14, 2004
    #4
  5. JT

    Harag Guest

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

    >
     
    Harag, Jan 14, 2004
    #5
  6. "Harag" <> wrote in message
    news:...
    >
    >
    > 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
     
    Chris Hohmann, Jan 14, 2004
    #6
  7. JT

    Harag Guest

    On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
    <> 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.
     
    Harag, Jan 15, 2004
    #7
  8. "Harag" <> wrote in message
    news:...
    > On Wed, 14 Jan 2004 15:29:40 -0800, "Chris Hohmann"
    > <> 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
     
    Chris Hohmann, Jan 15, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    6
    Views:
    70,029
    grizzleybear
    Apr 17, 2012
  2. Otto Wyss
    Replies:
    6
    Views:
    440
    Jeff Flinn
    Nov 9, 2004
  3. C Barr Leigh
    Replies:
    12
    Views:
    507
    Bruno Desthuilliers
    Mar 8, 2007
  4. (-: Dan :-)

    calling a optional parameter function

    (-: Dan :-), Oct 10, 2003, in forum: Javascript
    Replies:
    7
    Views:
    115
    Lasse Reichstein Nielsen
    Oct 13, 2003
  5. AzamSharp
    Replies:
    2
    Views:
    192
Loading...

Share This Page