title: How to return one and only one value from a PowerShell function?
body: <p>I've learned from <a href="https://stackoverflow.com/questions/10286164/powershell-function-return-value">this Stack Overflow question</a>, that PowerShell return semantics are different, let's say, from C#'s return semantics. Quote from the aforementioned question:</p>
  
  <blockquote>
    <p>PowerShell has really wacky return semantics - at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around: All output is captured, and returned.
    The return keyword really just indicates a logical exit point.</p>
  </blockquote>
  
  <p>Let's look at this example:</p>
  
  <pre><code>function Calculate
  {
     echo "Calculate"
     return 11
  }
  
  $result = Calculate
  </code></pre>
  
  <p>If you echo <code>$result</code> you will realise that something is not right. You'd expect this:</p>
  
  <pre><code>11
  </code></pre>
  
  <p>But the reality, what you actually see, is different:</p>
  
  <pre><code>Calculate
  11
  </code></pre>
  
  <p>So instead of getting back only the intended return value, you actually get back an array.</p>
  
  <p>You could get rid of the echo statements and not polluting the return value, but if you call another function from your function, which echoes something, then you're in trouble again.</p>
  
  <p>How can I write a PowerShell function that only returns one thing? If the built-in PowerShell functions do return only one value, why can't I do that?</p>
  
  <p>The workaround that I'm using now and I'd love to get rid of:</p>
  
  <pre><code>function Calculate
  {
     # Every function that returns has to echo something
     echo ""
     return 11
  }
  
  # The return values is the last value from the returning array
  $result = (Calculate)[-1]
  </code></pre>