Results 1 to 8 of 8

Thread: "doesn't return a value on all paths" ... help needed!

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    "doesn't return a value on all paths" ... help needed!

    I encountered the following warning while running aa code containing the Function below. The code runs fine despite the warning but it makes me nervous ... can anyone tell what to do about this:?

    Severity Code Description Project File Line Suppression State
    Warning BC42105 Function 'NbrIndex' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. newest atomizer 5-15-19 C:\Users\denny\source\repos\newest atomizer 5-15-19\newest atomizer 5-15-19\Module1.vb 291 Active

    Code:
        Public Function NbrIndex(ir As Integer, jz As Integer, nr As Integer, ByRef n As Integer, ByRef s_index As Integer,
        ByRef n_index As Integer, ByRef w_index As Integer, ByRef e_index As Integer, ByRef se_index As Integer,
        ByRef ne_index As Integer, ByRef sw_index As Integer, ByRef nw_index As Integer)
    
            Dim ip1, im1, jp1, jm1 As Integer
    
            ip1 = ir + 1
            im1 = ir - 1
            jp1 = jz + 1
            jm1 = jz - 1
    
            n = ir + jz * nr
    
            s_index = ir + jm1 * nr
            n_index = ir + jp1 * nr
            w_index = im1 + jz * nr
            e_index = ip1 + jz * nr
    
            'southeast index
            se_index = ip1 + jm1 * nr
            'northeast index
            ne_index = ip1 + jp1 * nr
            'southwest index
            sw_index = im1 + jm1 * nr
            'northwest index
            nw_index = im1 + jp1 * nr
    
        End Function

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: "doesn't return a value on all paths" ... help needed!

    You don't need a function. It doesn't return any value. It just modifies your ByRef arguments...

    Code:
    Public Sub NbrIndex(ByVal ir As Integer, ByVal jz As Integer, ByVal nr As Integer, ByRef n As Integer, ByRef s_index As Integer, _
        ByRef n_index As Integer, ByRef w_index As Integer, ByRef e_index As Integer, ByRef se_index As Integer, _
        ByRef ne_index As Integer, ByRef sw_index As Integer, ByRef nw_index As Integer)
    
            Dim ip1, im1, jp1, jm1 As Integer
    
            ip1 = ir + 1
            im1 = ir - 1
            jp1 = jz + 1
            jm1 = jz - 1
    
            n = ir + jz * nr
    
            s_index = ir + jm1 * nr
            n_index = ir + jp1 * nr
            w_index = im1 + jz * nr
            e_index = ip1 + jz * nr
    
            'southeast index
            se_index = ip1 + jm1 * nr
            'northeast index
            ne_index = ip1 + jp1 * nr
            'southwest index
            sw_index = im1 + jm1 * nr
            'northwest index
            nw_index = im1 + jp1 * nr
    
        End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    51

    Re: "doesn't return a value on all paths" ... help needed!

    The values are used all over the code … it returns matrix indices and it works … someone here told me I had to use ByRef to return the value to its calling sub. Thoughts?

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: "doesn't return a value on all paths" ... help needed!

    You are right to be nervous. That warning may be harmless, but it's always an issue...it just may be an issue you could ignore if you chose to. You won't know until you find it, though.

    The warning says what the problem is: There is at least one path of execution that the code could take which does not end by returning something. If you had branches (If statements and the like) in the code, then it can be hard to find. In this case, you don't even really have a function. You are passing most things ByRef and filling them in, which is often how you have to do things if you want multiple returns, as you do here (the other option would be to return a class or structure containing all the elements).

    So, the easy answer is that this is not a function, it's a sub. Change Function to Sub, and the warning will go away.

    At some point, you will want to turn Option Strict ON, which I believe will change that code from a warning to an error, and a different error, at that. I believe that with Option Strict ON, you are not allowed to have functions without a return type, which this doesn't have because it doesn't have a return. What Option Strict is doing is disallowing implicit conversions. Whenever you convert one type to a narrower type, you have to do so explicitly. For example, an integer can be turned into a double just fine, since an integer widens to a double (all integers can be doubles), but a double would have to be explicitly converted to an integer, since a double narrows to an integer (not all doubles can be integers without losing at least a bit of information). Explicit conversions are both faster and safer than implicit conversions, so Option Strict ON forces you to write better, safer, code. Of course, if you have a program already, and turn Option Strict ON, you will likely get a series of errors for any implicit conversions you were performing. If the program was working, those errors might be annoying, but by fixing them, the program will run ever so slightly faster (you'll only see that with VERY long calculations, at which point it might be 30-50% faster). You might also find an implicit conversion that worked fine only in some cases, and would otherwise have crashed the program, so you might find hidden bugs that had yet to bite.
    My usual boring signature: Nothing

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: "doesn't return a value on all paths" ... help needed!

    Quote Originally Posted by DennyGinson View Post
    The values are used all over the code … it returns matrix indices and it works … someone here told me I had to use ByRef to return the value to its calling sub. Thoughts?
    A function has a single return value which it returns with the Return [value] keyword. What you're doing is not a proper function. It works, because that is how ByRef works, but the function wasn't actually returning anything...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: "doesn't return a value on all paths" ... help needed!

    Quote Originally Posted by DennyGinson View Post
    The values are used all over the code … it returns matrix indices and it works … someone here told me I had to use ByRef to return the value to its calling sub. Thoughts?
    ByRef doesn't really return the value to the calling sub. Since you have integers, it works like this:

    For each integer argument passed ByVal, a little bit of temporary memory is created on the stack, and the value of the integer is copied into that memory, then the sub is run. That's pretty awkward if you want to see that value after the sub has finished, especially since that temporary memory is wiped away, so the values in it are lost. When you pass ByRef, you are creating the integer variables somewhere else in memory. There are still locations on the stack created for the variables, but rather than getting the value of the integer, what goes into those locations is the reference to the variable from the calling code. In other words, what is passed to the sub is the address in memory where that integer variable resides. Any changes made in the sub are made to that integer variable via the reference, rather than just to the temporary memory on the stack.

    So, with ByVal you are essentially saying, "here's some data, do with it what you want, because I won't be expecting to get anything back." With ByRef you are essentially saying, "here's a location in memory that holds an integer, you can use it, change it, or not, but once you're done, I'll be using whatever is in there."

    EDIT: I'm a bit slow on both posts.
    My usual boring signature: Nothing

  7. #7

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: "doesn't return a value on all paths" ... help needed!

    If you turned Option Strict On, which you should, then you wouldn't get that error the way you are because you couldn't have written your Sub and called it a Function. You would have to call it a Sub, because that is how you're using it.

    A Function should have a type, and return a value.

    Code:
    Function DoSomething( param1 As Integer, param2 As Integer) As Boolean
      Dim SomethingDone As Boolean = (param1 + param2 = 8)
    
      If SomethingDone Then
        Return True
      Else
        Return False
    End Function
    
    Dim Success As Boolean
    
    Success = DoSomthing(5, 3)
    
    If Success Then
      Messagebox.Show("Yay!")
    Else
      Messagebox.Show("Didn't work")
    End If
    Since you didn't give the method a type, and you don't return a value through a function Return statement (which is what the error is telling you, i.e. your function doesn't have a Return statement in all its execution paths, which in your case, there is only one path of execution), you should fix that.

    In this case, the fix is using a method that is a Sub, rather than a Function.

    In C, and its related languages, you don't have this distinction of different method syntax depending on whether the method returns a value or not. In C all methods are functions, but some of them are declared as void, meaning they are not going to return a value. In BASIC, a Sub is the equivalent of a void function, and is what you should always use if you don't plan to return a value that could be assigned to a variable on the left side of an assignment.

    Only use a function when returning a value.
    Modifying arguments passed as parameters to a method doesn't count as being a return value from a function.
    A function can only return one object.
    Both Subs or Functions can modify arguments passed through parameter variables, which is not equivalent to or meant by the phrase "a function's Return value".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width