PowerShell: Function Return Value and Statement

By Xah Lee. Date: . Last updated: .

Return value

# demo, of a function, anything not captured is returned.
# when there are multiple outputs, they are collected in a array as return value.

function ff {
    "something";
    4;
}

# call
$x = ff

$x
# something
# 4

# return type is array, because there are multiple outputs
$x.gettype().name
# Object[]
# suppress an output

function ff {
    "something";

    # suppress an output
    $null = 4;
}

# call
$x = ff

$x
# something

# return type is string
$x.gettype().name
# String

Return statement

function ff {
    3;
    return 4;
    5;
}

# call
$x = ff

$x
# 3
# 4

# return type is array
$x.gettype().name
# Object[]

PowerShell. Define Function