Results 1 to 3 of 3

Thread: Interesting Return behavior

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Interesting Return behavior

    I haven't seen anybody comment on this yet, and I haven't been able to come up with some use for it, but it does seem like there ought to be something clever that you could do with it:

    Consider these two short functions:

    VB Code:
    1. private function func1(ByRef n as integer) as integer
    2.  n=5
    3.  return n
    4.  
    5.  n=6
    6.  
    7. end function
    8.  
    9. private function func2(ByRef n as integer) as integer
    10.  try
    11.   n=5
    12.   return n
    13.  catch ex as exception
    14.   'Do nothing here.
    15.  Finally
    16.    n=6
    17.  end try
    18. end function

    Now consider using the two in this fashion:

    VB Code:
    1. public sub Main()
    2.   dim v as integer
    3.   dim n as integer
    4.  
    5.   n=0
    6.   v=func1(n)
    7.  
    8.    'What does n equal? What does v equal?
    9.  
    10.    n=0
    11.    v=func2(n)
    12.  
    13. end function

    It may appear that after calling func1 or func2, the variables v and n would be set to the same thing. Afterall, the only difference between the two functions is that func2 has error handling, which will never be called because no errors will arise in that code.

    However, the results will not be the same. After calling func1, both n and v will be set to 5. After calling func2, v is 5, but n is 6!

    In func1, the function returns at the return statement, and the n=6 line is never executed. In func2, the return simply sets the return to 5, but then the finally block executes, and n is set to 6. Since n is passed by reference, it is changed.

    I can't think of an obvious use for this, but it seems there is a window there to use a Finally block to do some odd things.

    Any ideas?
    My usual boring signature: Nothing

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Interesting Return behavior

    Yes, the Finally block is executed regardless of whether there is an error or a return statement. It would be where you would put your Close for a file or database connection, so you'd only need to have it in the one place.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Interesting Return behavior

    True, there are standard things that can be done there, but it seems like there ought to be some other use for a chunk of code that executes after the return statement. It's sort of like an OnClosing event in a window, but I have never actually used one of them for anything, either.

    On the other hand, I realize now that I have done some stupid things because I didn't realize this behavior.
    My usual boring signature: Nothing

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