title: Assigning Function Value Instead of Using Return Keyword, and its Effect on Synchronised Code Execution
body: <p>I am wondering if there is a difference between using</p>
  
  <pre><code>Public Function Foo() As Double
    Return 3.0
  End Function
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Public Function Foo() As Double
    Foo = 3.0
  End Function
  </code></pre>
  
  <p>but specifically with respect to code execution.</p>
  
  <p>I am attempting to manage a multithreaded application using synchronisation, and am not sure if I am capturing every lock and release correctly.</p>
  
  <p>I understand that code lines after 'Return' are not executed because the function loses focus, but what if the 'Return' is wrapped in a SyncLock block?</p>
  
  <pre><code>Public Function Foo() As Double
    SyncLock fooLock 
      Return 3.0
    End SyncLock
  End Function
  </code></pre>
  
  <p>Does the End SyncLock get called? Is the SyncLock block shorthand for:</p>
  
  <pre><code>Public Function Foo() As Double
    Dim result as Double
    Try
      Threading.Monitor.Enter(fooLock)
      result = 3.0
    Finally
      Threading.Monitor.Exit(fooLock)
    End Try
    Return result
  End Function
  </code></pre>
  
  <p>If my understanding is correct then the Finally block comes before the function releases focus, but alternatively if the Finally waits on the Return and subsequent code, then it may be a while before the Finally gets a chance, i.e.</p>
  
  <pre><code>Public Sub DoSomething()
    Dim a As Double = Foo
    ...Do other things
  End Sub
  
  
  Public Function Foo() As Double
    Try
      Threading.Monitor.Enter(fooLock)
      Return 3.0
  </code></pre>
  
  <p>...code returned to executes, 'a' is assigned to the return value of Foo, then perhaps all of the other tasks on the thread are done, then</p>
  
  <pre><code>  Finally
      Threading.Monitor.Exit(fooLock)
    End Try
  End Function
  </code></pre>
  
  <p>In this case my lock may have held for too long. For value types the first code would be acceptible, but for reference types, the first would release the lock then return a reference to the object and the consumer would have non-synced access to the value, the second may or may not work, depending on how much code is executed inbetween the break in the function.</p>
  
  <p>Could anyone help me straighten these concepts out?</p>