0

As we know, PowerShell has wacky return semantics.

Function return value in PowerShell shows there are two main ideas to wrap my head around:

  • All output is captured, and returned
  • The return keyword just indicates a logical exit point

Even things like reserving variables in outer scopes cause output, like [boolean]$isEnabled. Another good one is $someCollection.Add("toto") which spits the new collection count. Even Append() function causes output.

For example :

Function MyFunc {
        $res1 = new-object System.Text.StringBuilder
        $res1.Append("titi");
        $res2 = "toto"

        return $res2
}

$s = MyFunc
Write-Host $s

The output is : titi toto.

The expected output should be toto.

How to use a powershell function to return the expected value? (at least when viewed from a more traditional programming perspective)

Trevor65
  • 328
  • 1
  • 8

3 Answers3

1

Change

$res1.Append("titi");

to

$res1.Append("titi") | Out-Null

because the function returns every output which otherwise would be visible in the console.

TobyU
  • 3,718
  • 2
  • 21
  • 32
  • Is this the only solution? It seems very tedious to do this for each function returning a value in the console. – Trevor65 Sep 11 '18 at 12:04
  • $n = $res1.Append("titi") would work to. I'm always using Out-Null and don't no any other solution besides the two I've mentioned. – TobyU Sep 11 '18 at 12:37
0

if by using 'toto' you are trying to understand if your function succeeded, you could do

Function MyFunc {
    $res1 = new-object System.Text.StringBuilder
    $res1.Append("titi") | Out-Null
    return $?
}

"$?" returns a boolean if the previous command succeeded (true) or failed (false). so externally it would look like

$s = MyFunc
if ($s) {
    Write-Host "successful" -Foregroundcolor Green
}
else {
    Write-Error "unsuccessful"
}
ErikW
  • 386
  • 2
  • 11
0

When PowerShell was being developed, the team wanted to make it simple to use. But, it was confusing to people who know return from other languages. The implementation in classes is an attempt to rectify that mistake.

The return keyword works very differently in methods in PowerShell classes. It works like the return statements in other languages.

In a class method, the return keyword:

  • Exits the current scope.
  • Returns the associated object (return ).
  • Returns only the associated object.
  • The object that Return returns must match the return type of the method.

It is consistent with the return keyword and analogous keywords in other languages.

class ClassMyFunc
{
    [string] MyFunc
    {
        $res1 = new-object System.Text.StringBuilder
        $res1.Append("titi")
        $res2 = "toto"

        return $res2
    }
}

$cmf = New-Object -TypeName ClassMyFunc
$cmf.MyFunc()

The output is : toto, as expected. Using classes solved my problem, without having to search all functions returning a value in the console and piping it to Out-Null (as suggested by @TobyU).

Trevor65
  • 328
  • 1
  • 8