Concept Flow - Return values
Start Function
Execute Statements
Return Value Found?
No→Return Null
Yes
Return Value Sent Back
Function Ends
The function runs its code, then sends back a value if specified, or null if none.
function Get-Sum { param($a, $b) return $a + $b } $result = Get-Sum 3 4 Write-Output $result
| Step | Action | Variables | Return Value | Output |
|---|---|---|---|---|
| 1 | Call Get-Sum with a=3, b=4 | a=3, b=4 | None yet | None |
| 2 | Calculate a + b | a=3, b=4 | 7 | None |
| 3 | Return 7 from function | a=3, b=4 | 7 | None |
| 4 | Assign return value to $result | $result=7 | 7 | None |
| 5 | Write-Output $result | $result=7 | 7 | 7 |
| Variable | Start | After Step 1 | After Step 2 | After Step 4 | Final |
|---|---|---|---|---|---|
| a | undefined | 3 | 3 | undefined | undefined |
| b | undefined | 4 | 4 | undefined | undefined |
| result | undefined | undefined | undefined | 7 | 7 |
PowerShell functions use 'return' to send a value back. Return ends the function immediately. Without return, function returns null. Capture return value by assigning function call to a variable. Use returned value later in script.