question about returning value of a function

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

    #1

    question about returning value of a function

    Hi,

    i'm not sure to understand how a value is returned from a function.
    Look at this code below about a simple function that compares a value with
    values 1 from 5.
    I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
    return = -1. That's ok.
    If not, the function will return value z, but at the end of the for/next
    loop, the other return (return -1) will also be executed, no? The line
    'return -1' is outside of the loop and will be executed in all cases?

    Thanks
    Bob


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArg s)
    Handles Me.Load
    Dim x As Integer
    x = test(6)
    Response.Write( x)
    End Sub
    Function test(ByVal z As Integer) As Integer
    Dim i As Integer
    For i = 1 To 5
    If z = i Then
    Return z
    End If
    Next
    Return -1
    End Function


  • Just Me

    #2
    Re: question about returning value of a function


    For i = 1 To 5
    If z = i Then
    Return z
    Exit Function '// ADD THIS LINE
    End If
    Next



    "Bob" <.wrote in message news:u6%23$dcCL HHA.3564@TK2MSF TNGP02.phx.gbl. ..
    Hi,
    >
    i'm not sure to understand how a value is returned from a function.
    Look at this code below about a simple function that compares a value with
    values 1 from 5.
    I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
    return = -1. That's ok.
    If not, the function will return value z, but at the end of the for/next
    loop, the other return (return -1) will also be executed, no? The line
    'return -1' is outside of the loop and will be executed in all cases?
    >
    Thanks
    Bob
    >
    >
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Me.Load
    Dim x As Integer
    x = test(6)
    Response.Write( x)
    End Sub
    Function test(ByVal z As Integer) As Integer
    Dim i As Integer
    For i = 1 To 5
    If z = i Then
    Return z
    End If
    Next
    Return -1
    End Function
    >

    Comment

    • Bob

      #3
      Re: question about returning value of a function

      Thanks, but in the code i used as example, there was no EXIT function. So
      why will the Return -1 not be executed if the comparaison is true?

      "Just Me" <news.microsoft .comschreef in bericht
      news:%2355rCmCL HHA.5104@TK2MSF TNGP06.phx.gbl. ..
      >
      For i = 1 To 5
      If z = i Then
      Return z
      Exit Function '// ADD THIS LINE
      End If
      Next
      >
      >
      >
      "Bob" <.wrote in message news:u6%23$dcCL HHA.3564@TK2MSF TNGP02.phx.gbl. ..
      >Hi,
      >>
      >i'm not sure to understand how a value is returned from a function.
      >Look at this code below about a simple function that compares a value
      >with values 1 from 5.
      >I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
      >return = -1. That's ok.
      >If not, the function will return value z, but at the end of the for/next
      >loop, the other return (return -1) will also be executed, no? The line
      >'return -1' is outside of the loop and will be executed in all cases?
      >>
      >Thanks
      >Bob
      >>
      >>
      >Protected Sub Page_Load(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Me.Load
      > Dim x As Integer
      > x = test(6)
      > Response.Write( x)
      > End Sub
      > Function test(ByVal z As Integer) As Integer
      > Dim i As Integer
      > For i = 1 To 5
      > If z = i Then
      > Return z
      > End If
      > Next
      > Return -1
      > End Function
      >>
      >
      >

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: question about returning value of a function

        "Just Me" <news.microsoft .comschrieb:
        For i = 1 To 5
        If z = i Then
        Return z
        Exit Function '// ADD THIS LINE
        End If
        Next
        This line will never be executed because the method will be leaved when
        'Return z' is executed.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • Bob

          #5
          Re: question about returning value of a function

          Thanks

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atschr eef in bericht
          news:ugyTWADLHH A.1248@TK2MSFTN GP03.phx.gbl...
          "Just Me" <news.microsoft .comschrieb:
          > For i = 1 To 5
          > If z = i Then
          > Return z
          > Exit Function '// ADD THIS LINE
          > End If
          > Next
          >
          This line will never be executed because the method will be leaved when
          'Return z' is executed.
          >
          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

          Comment

          • Tom Leylan

            #6
            Re: question about returning value of a function

            The return statement is an instruction to "return" along with the value to
            return so yes you will get a -1 in your test sample. The line Return -1 is
            outside the loop but no it won't be executed in all cases. If the value
            passed is between 1 and 5 the earlier return statement will exit the
            function.

            That said (if you're developing good habits) you should reduce the number of
            return statements (ideally to one) and it should generally be at or near the
            end of the function. This can be accomplished by assigning a return
            variable (as needed in the routine) and returning it as the routine exits.


            "Bob" <.wrote in message news:u6%23$dcCL HHA.3564@TK2MSF TNGP02.phx.gbl. ..
            Hi,
            >
            i'm not sure to understand how a value is returned from a function.
            Look at this code below about a simple function that compares a value with
            values 1 from 5.
            I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
            return = -1. That's ok.
            If not, the function will return value z, but at the end of the for/next
            loop, the other return (return -1) will also be executed, no? The line
            'return -1' is outside of the loop and will be executed in all cases?
            >
            Thanks
            Bob
            >
            >
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As
            System.EventArg s) Handles Me.Load
            Dim x As Integer
            x = test(6)
            Response.Write( x)
            End Sub
            Function test(ByVal z As Integer) As Integer
            Dim i As Integer
            For i = 1 To 5
            If z = i Then
            Return z
            End If
            Next
            Return -1
            End Function
            >

            Comment

            Working...