sub or function

Discussion in 'ASP General' started by Roland Hall, Aug 2, 2005.

  1. That should read:

    In test 1.1 a _reference_ to the ...
     
    Chris Hohmann, Aug 4, 2005
    #21
    1. Advertisements

  2. Roland Hall

    Roland Hall Guest

    in message : Problem spotted - I'm an idiot.

    Hardly...

    : Of course, when we run test1() it is updating the variable y.
    :
    : Modified code:
    :
    : <%@ Language="VBScript" %>
    : <%
    :
    : Option Explicit
    :
    : Dim x
    :
    : function test1(x)
    : x = x + 10
    : test1 = x
    : End function
    :
    : function test2()
    : x = x + 1
    : test2 = x
    : end function
    :
    : sub test0()
    : dim y, z
    :
    : y = x
    :
    : z = test1(y)
    :
    : response.Write "Test1.1: " & z & "<br>"
    : response.Write "x = " & x & "<br>"
    : response.Write "y = " & y & "<br>"
    :
    : z = test1(y)
    :
    : response.Write "Test1.2: " & z & "<br>"
    : response.Write "x = " & x & "<br>"
    : response.Write "y = " & y & "<br>"
    :
    : z = test2()
    :
    : response.Write "Test2.1: " & z & "<br>"
    : response.Write "x = " & x & "<br>"
    : response.Write "y = " & y & "<br>"
    :
    : z = test2()
    :
    : response.Write "Test2.2: " & z & "<br>"
    : response.Write "x = " & x & "<br>"
    : response.Write "y = " & y & "<br>"
    : end sub
    :
    : x = 0
    :
    : response.Write "Pass1: <br>"
    : test0()
    :
    : response.Write "<br>Pass2: <br>"
    : test0()
    : %>

    Great example. Very helpful. Thanks.

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Aug 4, 2005
    #22
    1. Advertisements

  3. Roland Hall

    Roland Hall Guest

    in message
    : : > A local variable
    : > will also change if passed byRef to another sub/function?
    :
    : Yes.
    :
    : <%
    : Option Explicit
    : Function Foo(parameter_by_ref)
    : Dim return_value, local_variable
    : local_variable = parameter_by_ref
    : return_value = Bar(local_variable)
    : Foo = local_variable
    : End Function
    :
    : Function Bar(parameter_by_ref)
    : parameter_by_ref = parameter_by_ref + 1
    : Bar = "Hello World"
    : End Function
    :
    : Response.Write Foo(4)
    : %>

    Also helpful... thanks.

    This was very interesting to see:

    Option Explicit
    Function Foo(parameter_by_ref)
    Dim return_value, local_variable
    local_variable = parameter_by_ref
    Bar(local_variable)
    Foo = local_variable
    End Function

    sub Bar(parameter_by_ref)
    parameter_by_ref = parameter_by_ref + 1
    End sub

    wscript.echo Foo(4)

    Result: 4

    Option Explicit
    Function Foo(parameter_by_ref)
    Dim return_value, local_variable
    local_variable = parameter_by_ref
    Bar local_variable
    Foo = local_variable
    End Function

    sub Bar(parameter_by_ref)
    parameter_by_ref = parameter_by_ref + 1
    End sub

    wscript.echo Foo(4)

    Result: 5

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Aug 4, 2005
    #23
  4. In your first example, this line:

    Bar(local_variable)

    is interpreted by the parser as passing the expression "(local_variable)" to
    the Bar procedure. The parenthesis in this case causes the parser to
    interpret the argument being passed to the Bar procedure as a complex
    expression instead of a simple variable reference. As such, what's being
    passed in this case is a reference to the implicit/pseudo variable used to
    hold the result of evaluating the "(local_variable)" expression. It would be
    akin to this call:

    Bar local_variable + 1

    Or this one:

    Bar (local_variable + 1)

    Or this one:

    Bar 22/7

    You get the idea.
     
    Chris Hohmann, Aug 4, 2005
    #24
  5. Roland Hall

    CJM Guest


    No probs... In return, would you please do the Despatch Planning report that
    I was supposed to be doing while I did this?

    Cheers

    Chris
     
    CJM, Aug 4, 2005
    #25
  6. Roland Hall

    Roland Hall Guest

    :
    : >
    : > Great example. Very helpful. Thanks.
    :
    : No probs... In return, would you please do the Despatch Planning report
    that
    : I was supposed to be doing while I did this?

    Piece of cake. When would you like it completed?

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Aug 4, 2005
    #26
  7. Roland Hall

    Roland Hall Guest

    in message
    : : > "Chris Hohmann" wrote in message
    : > : > : : > : > A local variable
    : > : > will also change if passed byRef to another sub/function?
    : > :
    : > : Yes.
    : > :
    : > : <%
    : > : Option Explicit
    : > : Function Foo(parameter_by_ref)
    : > : Dim return_value, local_variable
    : > : local_variable = parameter_by_ref
    : > : return_value = Bar(local_variable)
    : > : Foo = local_variable
    : > : End Function
    : > :
    : > : Function Bar(parameter_by_ref)
    : > : parameter_by_ref = parameter_by_ref + 1
    : > : Bar = "Hello World"
    : > : End Function
    : > :
    : > : Response.Write Foo(4)
    : > : %>
    : >
    : > Also helpful... thanks.
    : >
    : > This was very interesting to see:
    : >
    : > Option Explicit
    : > Function Foo(parameter_by_ref)
    : > Dim return_value, local_variable
    : > local_variable = parameter_by_ref
    : > Bar(local_variable)
    : > Foo = local_variable
    : > End Function
    : >
    : > sub Bar(parameter_by_ref)
    : > parameter_by_ref = parameter_by_ref + 1
    : > End sub
    : >
    : > wscript.echo Foo(4)
    : >
    : > Result: 4
    : >
    : > Option Explicit
    : > Function Foo(parameter_by_ref)
    : > Dim return_value, local_variable
    : > local_variable = parameter_by_ref
    : > Bar local_variable
    : > Foo = local_variable
    : > End Function
    : >
    : > sub Bar(parameter_by_ref)
    : > parameter_by_ref = parameter_by_ref + 1
    : > End sub
    : >
    : > wscript.echo Foo(4)
    : >
    : > Result: 5
    :
    : In your first example, this line:
    :
    : Bar(local_variable)
    :
    : is interpreted by the parser as passing the expression "(local_variable)"
    to
    : the Bar procedure. The parenthesis in this case causes the parser to
    : interpret the argument being passed to the Bar procedure as a complex
    : expression instead of a simple variable reference. As such, what's being
    : passed in this case is a reference to the implicit/pseudo variable used to
    : hold the result of evaluating the "(local_variable)" expression. It would
    be
    : akin to this call:
    :
    : Bar local_variable + 1
    :
    : Or this one:
    :
    : Bar (local_variable + 1)
    :
    : Or this one:
    :
    : Bar 22/7
    :
    : You get the idea.

    Ya, I got it. I just never knew the ( ) were such an issue.

    --
    Roland Hall
    /* This information is distributed in the hope that it will be useful, but
    without any warranty; without even the implied warranty of merchantability
    or fitness for a particular purpose. */
    Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
    WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
    MSDN Library - http://msdn.microsoft.com/library/default.asp
     
    Roland Hall, Aug 4, 2005
    #27
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.