sub or function

C

Chris Hohmann

Chris Hohmann said:
In test 1.1 the a _reference_ to the local y variable in test0 is being
passed to the test1 function.

That should read:

In test 1.1 a _reference_ to the ...
 
R

Roland Hall

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
 
R

Roland Hall

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
 
C

Chris Hohmann

Roland Hall said:
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.
 
C

CJM

Roland Hall said:
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?

Cheers

Chris
 
R

Roland Hall

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

Roland Hall

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
 

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

Ask a Question

Similar Threads

VBScript function returning multiple values 17
the perfect function 4
ASP design question 4
Compressed Zipped Folder 6
screen scraping 4
Application question 2
replace text 2
Objects and collections 4

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top