sub or function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roland Hall

    #16
    Re: sub or function

    "CJM" <cjmnews04@news group.nospam> wrote in message
    news:%230eGH7Mm FHA.3312@tk2msf tngp13.phx.gbl. ..
    :
    : "Roland Hall" <nobody@nowhere > wrote in message
    : news:OZjYjXHmFH A.2180@TK2MSFTN GP15.phx.gbl...
    : >
    : > Are you saying a passed variable to a sub, if the value changes will
    : > change
    : > the value of a global variable? Isn't that only true if it's passed
    : > ByRef?
    :
    : However, if you were talking about a Global variable, there would be no
    : reason to pass it as a parameter because it already accessible because it
    : is...er.. Global!
    :
    : I hope this hasn't muddied the water any further!

    No problem.

    What if it is passed from another sub/function?

    function myincfunc(x)
    myincfunc= x + 1
    end function

    sub tomorrow(x)
    dim thisday, nextday
    thisday = x
    nextday = myincfunc(thisd ay)
    Response.Write "Tomorrow's secret number is: " & nextday
    end sub

    dim x
    x = 4
    tomorrow x

    x is global
    sub x is byRef, assigned byVal to thisday, passed byRef to func x,
    incremented and returned

    Did any initial variables change?

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


    Comment

    • Roland Hall

      #17
      Re: sub or function

      "Bob Lehmann" wrote in message news:efYuPqOmFH A.3288@TK2MSFTN GP09.phx.gbl...
      : >> Isn't that only true if it's passed ByRef?
      : The default is ByRef in VBScript. So, unless you you explicitly pass it
      : ByVal, it will be changed.

      I know it [global variable] changes because I've been caught with my pants
      down on this issue before and had to use ByVal to get out of it but it was a
      global variable I was passing, which was not required. A local variable
      will also change if passed byRef to another sub/function?

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


      Comment

      • CJM

        #18
        Re: sub or function

        Problem spotted - I'm an idiot.

        Of course, when we run test1() it is updating the variable y.

        Modified code:

        <%@ Language="VBScr ipt" %>
        <%

        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()
        %>


        Comment

        • Chris Hohmann

          #19
          Re: sub or function

          "Roland Hall" <nobody@nowhere > wrote in message
          news:uc1wfpQmFH A.3916@TK2MSFTN GP10.phx.gbl...[color=blue]
          > A local variable
          > will also change if passed byRef to another sub/function?[/color]

          Yes.

          <%
          Option Explicit
          Function Foo(parameter_b y_ref)
          Dim return_value, local_variable
          local_variable = parameter_by_re f
          return_value = Bar(local_varia ble)
          Foo = local_variable
          End Function

          Function Bar(parameter_b y_ref)
          parameter_by_re f = parameter_by_re f + 1
          Bar = "Hello World"
          End Function

          Response.Write Foo(4)
          %>


          Comment

          • Chris Hohmann

            #20
            Re: sub or function

            "CJM" <cjmnews04@news group.nospam> wrote in message
            news:uhw6oBRmFH A.3828@TK2MSFTN GP12.phx.gbl...[color=blue]
            >
            > "Roland Hall" <nobody@nowhere > wrote in message
            > news:%23LaytnQm FHA.2060@tk2msf tngp13.phx.gbl. ..[color=green]
            >>
            >> What if it is passed from another sub/function?
            >>
            >> function myincfunc(x)
            >> myincfunc= x + 1
            >> end function
            >>
            >> sub tomorrow(x)
            >> dim thisday, nextday
            >> thisday = x
            >> nextday = myincfunc(thisd ay)
            >> Response.Write "Tomorrow's secret number is: " & nextday
            >> end sub
            >>
            >> dim x
            >> x = 4
            >> tomorrow x
            >>
            >> x is global
            >> sub x is byRef, assigned byVal to thisday, passed byRef to func x,
            >> incremented and returned
            >>
            >> Did any initial variables change?[/color]
            >
            > AFAIK...
            >
            > The x in myincfunc is local to the function - it is not the same as the
            > global variable x. So in this instance the global variable x does not
            > change.
            >
            > If we change the function to:
            >
            > function myincfunc()
            > myincfunc= x + 1
            > end function
            >
            > ...so there is no incoming parameter, x the global variable x would be
            > incremented each time the function is called.
            >
            > [10 minute interlude...]
            >
            > Right! I've created a test page. It kind of proved what I was saying with
            > one exception which I dont understand!
            >
            > Here's the code:
            >
            > <%@ Language="VBScr ipt" %>
            > <%
            >
            > 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>"
            >
            > z = test1(y)
            >
            > response.Write "Test1.2: " & z & "<br>"
            > response.Write "x = " & x & "<br>"
            >
            > z = test2()
            >
            > response.Write "Test2.1: " & z & "<br>"
            > response.Write "x = " & x & "<br>"
            >
            > z = test2()
            >
            > response.Write "Test2.2: " & z & "<br>"
            > response.Write "x = " & x & "<br>"
            > end sub
            >
            > x = 0
            >
            > response.Write "Pass1: <br>"
            > test0()
            >
            > response.Write "<br>Pass2: <br>"
            > test0()
            > %>
            >
            > And here are my results:
            >
            > Pass1:
            > Test1.1: 10
            > x = 0
            > Test1.2: 20
            > x = 0
            > Test2.1: 1
            > x = 1
            > Test2.2: 2
            > x = 2
            >
            > Pass2:
            > Test1.1: 12
            > x = 2
            > Test1.2: 22
            > x = 2
            > Test2.1: 3
            > x = 3
            > Test2.2: 4
            > x = 4
            >
            > It all behaves as I would expect, except for test 1.2. In these cases, I
            > would expect the returned result to be the same as Test1.1 UNLESS the
            > global variable x is indeed being updated. However, we can see from the
            > next line that x only appears to be updated by the test2 function.
            >
            > 3 Possibilities:
            > - A Bug: possible but doubtful
            > - A cock-up in my code: usually very likely
            > - Correct behaviour though for unknown reasons: is this the case?
            >
            >
            > Any thoughts?
            >
            > Chris[/color]

            Everything is behaving as it should. In test 1.1 the a _reference_ to the
            local y variable in test0 is being passed to the test1 function. Since it's
            a reference, the local y variable in test0 gets incremented to 10. When the
            local y variable is pass by _reference_ to the test1 function for a second
            time in test 1.2, it is again incremented by 10, resulting in a value of 20.
            Basically, all parameters, regardless of scope, are passed by reference
            unless explicitly told otherwise.


            Comment

            • Chris Hohmann

              #21
              Re: sub or function

              "Chris Hohmann" <nospam@thankyo u.com> wrote in message
              news:OVtylNRmFH A.2916@TK2MSFTN GP14.phx.gbl...[color=blue]
              > In test 1.1 the a _reference_ to the local y variable in test0 is being
              > passed to the test1 function.[/color]

              That should read:

              In test 1.1 a _reference_ to the ...


              Comment

              • Roland Hall

                #22
                Re: sub or function

                "CJM" wrote in message news:OoA0ZFRmFH A.1372@TK2MSFTN GP10.phx.gbl...
                : Problem spotted - I'm an idiot.

                Hardly...

                : Of course, when we run test1() it is updating the variable y.
                :
                : Modified code:
                :
                : <%@ Language="VBScr ipt" %>
                : <%
                :
                : 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


                Comment

                • Roland Hall

                  #23
                  Re: sub or function

                  "Chris Hohmann" wrote in message
                  news:e2tagFRmFH A.2080@TK2MSFTN GP14.phx.gbl...
                  : "Roland Hall" <nobody@nowhere > wrote in message
                  : news:uc1wfpQmFH A.3916@TK2MSFTN GP10.phx.gbl...
                  : > A local variable
                  : > will also change if passed byRef to another sub/function?
                  :
                  : Yes.
                  :
                  : <%
                  : Option Explicit
                  : Function Foo(parameter_b y_ref)
                  : Dim return_value, local_variable
                  : local_variable = parameter_by_re f
                  : return_value = Bar(local_varia ble)
                  : Foo = local_variable
                  : End Function
                  :
                  : Function Bar(parameter_b y_ref)
                  : parameter_by_re f = parameter_by_re f + 1
                  : Bar = "Hello World"
                  : End Function
                  :
                  : Response.Write Foo(4)
                  : %>

                  Also helpful... thanks.

                  This was very interesting to see:

                  Option Explicit
                  Function Foo(parameter_b y_ref)
                  Dim return_value, local_variable
                  local_variable = parameter_by_re f
                  Bar(local_varia ble)
                  Foo = local_variable
                  End Function

                  sub Bar(parameter_b y_ref)
                  parameter_by_re f = parameter_by_re f + 1
                  End sub

                  wscript.echo Foo(4)

                  Result: 4

                  Option Explicit
                  Function Foo(parameter_b y_ref)
                  Dim return_value, local_variable
                  local_variable = parameter_by_re f
                  Bar local_variable
                  Foo = local_variable
                  End Function

                  sub Bar(parameter_b y_ref)
                  parameter_by_re f = parameter_by_re f + 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


                  Comment

                  • Chris Hohmann

                    #24
                    Re: sub or function

                    "Roland Hall" <nobody@nowhere > wrote in message
                    news:eEpSNoRmFH A.3336@tk2msftn gp13.phx.gbl...[color=blue]
                    > "Chris Hohmann" wrote in message
                    > news:e2tagFRmFH A.2080@TK2MSFTN GP14.phx.gbl...
                    > : "Roland Hall" <nobody@nowhere > wrote in message
                    > : news:uc1wfpQmFH A.3916@TK2MSFTN GP10.phx.gbl...
                    > : > A local variable
                    > : > will also change if passed byRef to another sub/function?
                    > :
                    > : Yes.
                    > :
                    > : <%
                    > : Option Explicit
                    > : Function Foo(parameter_b y_ref)
                    > : Dim return_value, local_variable
                    > : local_variable = parameter_by_re f
                    > : return_value = Bar(local_varia ble)
                    > : Foo = local_variable
                    > : End Function
                    > :
                    > : Function Bar(parameter_b y_ref)
                    > : parameter_by_re f = parameter_by_re f + 1
                    > : Bar = "Hello World"
                    > : End Function
                    > :
                    > : Response.Write Foo(4)
                    > : %>
                    >
                    > Also helpful... thanks.
                    >
                    > This was very interesting to see:
                    >
                    > Option Explicit
                    > Function Foo(parameter_b y_ref)
                    > Dim return_value, local_variable
                    > local_variable = parameter_by_re f
                    > Bar(local_varia ble)
                    > Foo = local_variable
                    > End Function
                    >
                    > sub Bar(parameter_b y_ref)
                    > parameter_by_re f = parameter_by_re f + 1
                    > End sub
                    >
                    > wscript.echo Foo(4)
                    >
                    > Result: 4
                    >
                    > Option Explicit
                    > Function Foo(parameter_b y_ref)
                    > Dim return_value, local_variable
                    > local_variable = parameter_by_re f
                    > Bar local_variable
                    > Foo = local_variable
                    > End Function
                    >
                    > sub Bar(parameter_b y_ref)
                    > parameter_by_re f = parameter_by_re f + 1
                    > End sub
                    >
                    > wscript.echo Foo(4)
                    >
                    > Result: 5[/color]

                    In your first example, this line:

                    Bar(local_varia ble)

                    is interpreted by the parser as passing the expression "(local_variabl e)" 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_variabl e)" 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.


                    Comment

                    • CJM

                      #25
                      Re: sub or function


                      "Roland Hall" <nobody@nowhere > wrote in message[color=blue]
                      >
                      > Great example. Very helpful. Thanks.
                      >[/color]


                      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


                      Comment

                      • Roland Hall

                        #26
                        Re: sub or function

                        "CJM" <cjmnews04@yaho o.co.uk> wrote in message
                        news:3lf6kcF11n 7t8U1@individua l.net...
                        :
                        : "Roland Hall" <nobody@nowhere > wrote in message
                        : >
                        : > 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


                        Comment

                        • Roland Hall

                          #27
                          Re: sub or function

                          "Chris Hohmann" wrote in message
                          news:uMA6y7RmFH A.3316@TK2MSFTN GP14.phx.gbl...
                          : "Roland Hall" <nobody@nowhere > wrote in message
                          : news:eEpSNoRmFH A.3336@tk2msftn gp13.phx.gbl...
                          : > "Chris Hohmann" wrote in message
                          : > news:e2tagFRmFH A.2080@TK2MSFTN GP14.phx.gbl...
                          : > : "Roland Hall" <nobody@nowhere > wrote in message
                          : > : news:uc1wfpQmFH A.3916@TK2MSFTN GP10.phx.gbl...
                          : > : > A local variable
                          : > : > will also change if passed byRef to another sub/function?
                          : > :
                          : > : Yes.
                          : > :
                          : > : <%
                          : > : Option Explicit
                          : > : Function Foo(parameter_b y_ref)
                          : > : Dim return_value, local_variable
                          : > : local_variable = parameter_by_re f
                          : > : return_value = Bar(local_varia ble)
                          : > : Foo = local_variable
                          : > : End Function
                          : > :
                          : > : Function Bar(parameter_b y_ref)
                          : > : parameter_by_re f = parameter_by_re f + 1
                          : > : Bar = "Hello World"
                          : > : End Function
                          : > :
                          : > : Response.Write Foo(4)
                          : > : %>
                          : >
                          : > Also helpful... thanks.
                          : >
                          : > This was very interesting to see:
                          : >
                          : > Option Explicit
                          : > Function Foo(parameter_b y_ref)
                          : > Dim return_value, local_variable
                          : > local_variable = parameter_by_re f
                          : > Bar(local_varia ble)
                          : > Foo = local_variable
                          : > End Function
                          : >
                          : > sub Bar(parameter_b y_ref)
                          : > parameter_by_re f = parameter_by_re f + 1
                          : > End sub
                          : >
                          : > wscript.echo Foo(4)
                          : >
                          : > Result: 4
                          : >
                          : > Option Explicit
                          : > Function Foo(parameter_b y_ref)
                          : > Dim return_value, local_variable
                          : > local_variable = parameter_by_re f
                          : > Bar local_variable
                          : > Foo = local_variable
                          : > End Function
                          : >
                          : > sub Bar(parameter_b y_ref)
                          : > parameter_by_re f = parameter_by_re f + 1
                          : > End sub
                          : >
                          : > wscript.echo Foo(4)
                          : >
                          : > Result: 5
                          :
                          : In your first example, this line:
                          :
                          : Bar(local_varia ble)
                          :
                          : is interpreted by the parser as passing the expression "(local_variabl e)"
                          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_variabl e)" 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


                          Comment

                          Working...