function not functioning in a loop

Discussion in 'ASP General' started by Mike, Sep 25, 2003.

  1. Mike

    Mike Guest

    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
    Mike, Sep 25, 2003
    #1
    1. Advertising

  2. "Mike" wrote:
    >
    > 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.
    Dave Anderson, Sep 25, 2003
    #2
    1. Advertising

  3. Mike

    Mike Guest

    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



    "Dave Anderson" <> wrote in message
    news:...
    > "Mike" wrote:
    > >
    > > 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.
    >
    >
    Mike, Sep 25, 2003
    #3
  4. Mike

    Mike Guest

    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


    "Mike" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Dave Anderson" <> wrote in message
    > news:...
    > > "Mike" wrote:
    > > >
    > > > 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.
    > >
    > >

    >
    >
    Mike, Sep 25, 2003
    #4
  5. 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
    %>
    Aaron Bertrand - MVP, Sep 25, 2003
    #5
  6. Mike

    Ray at Guest

    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


    "Mike" <> wrote in message
    news:%...
    > 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
    >
    >
    Ray at, Sep 25, 2003
    #6
  7. 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]

    WAND-TV (ABC Affiliate)
    www.wandtv.com




    "Mike" <> wrote in message
    news:...
    > 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
    >
    >
    >
    > "Dave Anderson" <> wrote in message
    > news:...
    > > "Mike" wrote:
    > > >
    > > > 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.
    > >
    > >

    >
    >
    Phillip Windell, Sep 25, 2003
    #7
  8. Mike

    Ray at Guest

    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


    "Phillip Windell" <> wrote in message
    news:%...
    > 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]
    >
    > WAND-TV (ABC Affiliate)
    > www.wandtv.com
    >
    >
    >
    Ray at, Sep 25, 2003
    #8
  9. Mike

    Hum Hum Guest

    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

    Mike wrote:

    > 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
    >
    >
    >
    > "Dave Anderson" <> wrote in message
    > news:...
    >
    >>"Mike" wrote:
    >>
    >>>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.
    >>
    >>

    >
    >
    >
    Hum Hum, Sep 25, 2003
    #9
  10. Mike

    dlbjr Guest

    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.
    dlbjr, Sep 25, 2003
    #10
  11. Mike

    Mike Guest

    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


    "Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
    news:...
    > 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
    >
    >
    > "Mike" <> wrote in message
    > news:%...
    > > 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
    > >
    > >

    >
    >
    Mike, Sep 26, 2003
    #11
    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. sadf
    Replies:
    4
    Views:
    803
    Bob Carsto
    Nov 14, 2003
  2. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    Enable property of a Panel not functioning

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Feb 7, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    347
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Feb 7, 2005
  3. surveyor04
    Replies:
    0
    Views:
    366
    surveyor04
    Feb 12, 2005
  4. Mike

    function not functioning in a loop

    Mike, Sep 25, 2003, in forum: ASP General
    Replies:
    1
    Views:
    102
    PB4FUN
    Sep 25, 2003
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page