function not functioning in a loop

M

Mike

This may be stupid but


Function TestFunction(strValue)
TestFunction = strValue + 1
End Function

y = 1

For x = 1 to 10
Response.write TestFunction(y) & "<br>"
y = y + 1
Next

produces
2
3
4
5
6
7
8
9
10
11

Shouldn't it produce

2
4
6
etc.

It seems like the function is only running the first time through the loop.
What am I missing?

Thanks
Mike
 
D

Dave Anderson

Mike said:
Function TestFunction(strValue)
TestFunction = strValue + 1
End Function

y = 1

For x = 1 to 10
Response.write TestFunction(y) & "<br>"
y = y + 1
Next

produces
2
3
4
5
6
7
8
9
10
11

Doesn't it actually produce the following?

2<BR>3<BR>4<BR>5<BR>6<BR>7<BR>8<BR>9<BR>10<BR>11<BR>

And why do you confuse the matter by suggesting that the function parameter
should be a String? I assume you know + can act as a concatenation operator
in VBScript.

Shouldn't it produce

2
4
6
etc.

No. You seem to be assuming that your function changes the value of y. It
does not.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
 
M

Mike

I am not assuming the function changes the value, the y = y + 1 line does
that. The strValue doesn't confuse me because this isn't the real function
it just demonstrates the point.

Mike
 
M

Mike

Damn. It's time for alcohol. Looking at the same page with these old eyes.
You were right thanks for making me look harder. I think it's time for a
career change.

Mike
 
A

Aaron Bertrand - MVP

Maybe this will illustrate Dave's point better:

<%
Function TestFunction(strValue)
TestFunction = strValue + 1
End Function

y = 1

For x = 1 to 10
Response.write "<b>" & x & "</b><br>"
Response.write y & "<br>" ' note that y = x every time!!!
response.write y + 1 & "<br>"
Response.write TestFunction(y) & "<p>"
y = y + 1
Next
%>

If you want testfunction to remember what it did to y, you can do this:

<%
Function TestFunction(strValue)
y = strValue + 1
TestFunction = y
End Function
%>
 
R

Ray at

Step through it manually:

x = 1
Response.Write TestFunction(y) ''y = 1, so written = 2
y = y + 1 '''=2
x = 2
Response.Write TestFunction(y) ''y = 2, so written = 3
y = y + 1 '''=3
x = 3
Response.Write TestFunction(y) ''y = 3, so written = 4

''etc.

Try this:


<%

Sub TestSub(ByRef iValue)
iValue = iValue + 1
End Sub

y = 1

For x = 1 to 10
Call TestSub(y)
Response.Write y & "<br>"
y = y + 1
Next

%>

Ray at work
 
P

Phillip Windell

Mike,
It is doing exactly what you are telling it. It loops 10 times. Y
equals 2 the first time around and increments by 1 each time until it
finished at 11. If you wanted it to count by two's (2, 4, 6, etc.) the
function should be:
TestFunction = strValue + 2

It will then begin with 2 and end with,...um..I think 22.


--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 
R

Ray at

Wouldn't that just make it start at 3 instead of 2 and still just increment
by 1? I believe so. He'd have to do y = y + 2.

Ray at work
 
H

Hum Hum

IF you want 2 4 6 8
Try :

Function Truc(Valeur)
Truc = Valeur + 1
End Function
y = 1

For x = 1 to 10
Response.write Truc(y) & "<br>"
y = Truc(y) + 1
Next
 
D

dlbjr

Mike,

'Mike the function does not add one to y and save in y
'It just adds 1 to y and prints then leaves y alone.
'try this

<%
Function TestFunction(ByRef strValue)
TestFunction = strValue + 1
End Function

y = 0
For x = 1 to 10
y = TestFunction(y)
y = y + 1
Response.write "y = " & y & "<br>"
Next
%>

dlbjr

Unambit from meager knowledge of inane others,
engender uncharted sagacity.
 
M

Mike

Thanks to all. It was just a brain fart yesterday. I had a function that
wasn't working. So I blamed the loop. I then wrote the test function and
it worked correctly as has been stated here but I was too blind or stupid to
see it. We all have those days don't we:-(

Thanks again

Mike
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top