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.