Author Posts

August 6, 2018 at 5:43 pm

Just about every blog or post I've run into says the 'Return' statement in PS is really not needed, that all it does is to stop execution and pass control back to the calling routine. More relevant to this is that "everything that is outputted in the called Function is returned to the calling routine: as an array." Q1 – WHERE is the array?
With these impressions, rightly or wrongly, I cannot see how Return is "not needed":

function Get-Sum([int]$value)
  $result = $value + $value
  return $result
$value = Read-Host 'Enter a value'
$result = Get-Sum $value
Write-Output "$value + $value = $result"

In the above example, if I removed the 'return $result' statement in the function,
I would get from the Write-Output statement the ff output:
11 + 11 =
while with the 'return $result' I would get:
11 + 11 = 121
Q2 – I must be missing something about how RETURN works in a PS Function, or my example has a fundamental flaw, or ??
Would appreciate any tips or advice.

August 6, 2018 at 5:45 pm

Ah, the Return keyword.

No, it isn't necessary. Outside of a v5+ class, the Return keyword basically just emits to Write-Output and exits. It's "syntax sugar," and it doesn't behave the way Return does in any other language. It's generally to be avoided. Snover regrets it. Its existence encourages programming patterns that, in PowerShell, are incorrect.

Just use Write-Output.

August 6, 2018 at 6:23 pm

Or, even better (for performance-oriented functions) omit it entirely and drop to output.

To powershell, both of the following are equivalent:

Write-Output $Value

Write-Output introduces some additional overhead that is already handled by powershell's output stream anyway, so there's rarely a need to specify Write-Output at all.

It's worth noting that return does follow some different patterns to Write-Output. For example, the return statement in the middle of a function, does exit the function as well as output what is requested. But it also sends along anything previously output in the function:

function Write-Data {
  "Write 1" # Gets sent to output, no cmdlet required

PS> Write-Data
Write 1
function Write-Data {
  "Write 1"
  return "Write 2"
  Write-Output "Write 3"

PS> Write-Data
Write 1
Write 2

As a result, return is a keyword (as Don mentions) best avoided in PowerShell. When writing PS classes, it is required — for functions and other ordinary script blocks, though, just avoid it. There are some cases where you do want to exit a function early. In those cases, to avoid being unclear, it is perhaps best to simply use the return statement alone, to indicate that that is its only purpose. If you pass it output, it may (for longer functions) appear that that is intended to be the only output, but it will not be.

# Probably best avoided
function Write-Data {
  Write-Output "Write 1"
  return "Write 2"

# probably OK, but use sparingly and only when absolutely necessary
function Write-Data {
  "Write 1"
  "Write 2"

That would be my personal preference. Yes, it does work in the first way, but for ease of maintainability I'd advise against it.