## 长说明

### 语法

``````return [<expression>]
``````

``````return
return \$a
return (2 + \$a)
``````

### 示例

``````function MultiplyEven
{
param(\$number)

if (\$number % 2) { return "\$number is not even" }
\$number * 2
}

1..10 | ForEach-Object {MultiplyEven -Number \$_}
``````
``````1 is not even
4
3 is not even
8
5 is not even
12
7 is not even
16
9 is not even
20
``````

``````\$a
return
``````

``````return \$a
``````

``````function calculation {
param (\$value)

"Please wait. Working on calculation..."
\$value += 73
return \$value
}

\$a = calculation 14
``````

“请等待。 正在处理计算...”不显示字符串。 而是将其 `\$a` 分配给变量，如以下示例所示：

``````PS> \$a
Please wait. Working on calculation...
87
``````

``````function calculation {
param (\$value)

Write-Information "Please wait. Working on calculation..." -InformationAction Continue
\$value += 73
return \$value
}

\$a = calculation 14
``````
``````Please wait. Working on calculation...
C:\PS> \$a
87
``````

### 返回值和管道

``````function Test-Return
{
\$array = 1,2,3
return \$array
}
Test-Return | Measure-Object
``````
``````Count    : 3
Average  :
Sum      :
Maximum  :
Minimum  :
Property :
``````

• 一元数组表达式

利用一元表达式，可以将返回值作为单个对象发送到管道，如以下示例所示。

``````function Test-Return
{
\$array = 1,2,3
return (, \$array)
}
Test-Return | Measure-Object
``````
``````Count    : 1
Average  :
Sum      :
Maximum  :
Minimum  :
Property :
``````
• `Write-Output` 具有 NoEnumerate 参数。

还可以将 `Write-Output` cmdlet 与 NoEnumerate 参数配合使用。 以下示例使用 `Measure-Object` cmdlet 对关键字 `return` 从示例函数发送到管道的对象进行计数。

``````function Test-Return
{
\$array = 1, 2, 3
return Write-Output -NoEnumerate \$array
}

Test-Return | Measure-Object
``````
``````Count    : 1
Average  :
Sum      :
Maximum  :
Minimum  :
Property :
``````