📖 Scenario: You are writing a simple PowerShell script to calculate the area of rectangles. You want to create a function that takes the width and height as inputs and returns the area.
🎯 Goal: Build a PowerShell function that returns the area of a rectangle given its width and height, then display the result.
📋 What You'll Learn
Create a function named Get-Area that takes two parameters: width and height.
Inside the function, calculate the area by multiplying width and height.
Return the calculated area from the function.
Call the function with specific values and print the returned area.
💡 Why This Matters
🌍 Real World
Functions that return values are used in scripts to perform calculations or data processing and send results back for further use.
💼 Career
Understanding how to write functions with return values is essential for automating tasks and creating reusable code in IT and DevOps roles.
Progress0 / 4 steps
1
Create the function skeleton
Create a function called Get-Area with two parameters: width and height. Do not add any code inside the function yet.
PowerShell
Hint
Use the function keyword and param() block to define parameters.
2
Calculate the area inside the function
Inside the Get-Area function, create a variable called $area that multiplies $width and $height.
PowerShell
Hint
Use $area = $width * $height to calculate the area.
3
Return the area from the function
Add a return statement inside the Get-Area function to return the value of $area.
PowerShell
Hint
Use return $area to send the result back.
4
Call the function and display the result
Call the Get-Area function with width 5 and height 10, store the result in a variable called $result, then print $result.
PowerShell
Hint
Use $result = Get-Area -width 5 -height 10 and Write-Output $result to show the area.
Practice
(1/5)
1. What does the return keyword do in a PowerShell function?
easy
A. It prints a message to the screen.
B. It sends a value back to the place where the function was called.
C. It stops the script from running completely.
D. It creates a new variable inside the function.
Solution
Step 1: Understand the purpose of return
The return keyword in PowerShell is used inside a function to send a value back to the caller.
Step 2: Differentiate from other actions
It does not stop the entire script, print messages, or create variables; it only sends back a result.
Final Answer:
It sends a value back to the place where the function was called. -> Option B
Quick Check:
Return keyword = sends value back [OK]
Hint: Remember: return sends data back from a function [OK]
Common Mistakes:
Thinking return stops the whole script
Confusing return with Write-Host output
Assuming return creates variables
2. Which of the following is the correct syntax to return a value from a PowerShell function?
easy
A. return $value
B. send $value
C. output $value
D. yield $value
Solution
Step 1: Identify the correct keyword for returning values
In PowerShell, the keyword to return a value from a function is return.
Step 2: Check syntax correctness
The correct syntax is return $value. Other options like send, output, or yield are not valid PowerShell keywords for returning values.
Final Answer:
return $value -> Option A
Quick Check:
Return syntax = return $value [OK]
Hint: Use 'return' keyword to send back values [OK]
Common Mistakes:
Using non-existent keywords like send or yield
Omitting the return keyword
Confusing return with Write-Output
3. What will be the output of this PowerShell code?
A. The function does not use the return keyword to send back the sum.
B. The parameters are not declared correctly.
C. The addition operator '+' is invalid in PowerShell.
D. The variable $sum is not assigned properly.
Solution
Step 1: Check how the function returns the result
The function uses Write-Host to display the sum but does not use return to send the value back to the caller.
Step 2: Understand the difference between output and return
Write-Host sends output to the pipeline but does not return a value from the function in the expected way for assignment.
Final Answer:
The function does not use the return keyword to send back the sum. -> Option A
Quick Check:
Return keyword needed to send value back [OK]
Hint: Use return to send values back, not just Write-Host [OK]
Common Mistakes:
Confusing Write-Host with return
Thinking parameters are wrong
Believing '+' is invalid operator
5. You want to create a PowerShell function that returns the first non-empty string from a list of inputs. Which function correctly returns the first non-empty string?
hard
A. function Get-FirstNonEmpty {
param([string[]]$inputs)
return $inputs[0]
}
B. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item -eq '') { return $item }
}
return $null
}
C. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if (-not $item) { return $item }
}
return 'empty'
}
D. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item) { return $item }
}
return ''
}
Solution
Step 1: Understand the goal
The function should return the first string that is not empty or null from the input list.
Step 2: Analyze each option
function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item) { return $item }
}
return ''
} checks if the item is truthy (non-empty), then returns it immediately, else returns empty string if none found. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if ($item -eq '') { return $item }
}
return $null
} returns empty strings, which is wrong. function Get-FirstNonEmpty {
param([string[]]$inputs)
foreach ($item in $inputs) {
if (-not $item) { return $item }
}
return 'empty'
} returns empty or null values, opposite of requirement. function Get-FirstNonEmpty {
param([string[]]$inputs)
return $inputs[0]
} always returns first item without checking if empty.
Final Answer:
if ($item) { return $item } -> Option D
Quick Check:
Return first truthy string using if ($item) [OK]
Hint: Return first truthy item with if ($item) inside loop [OK]
Common Mistakes:
Returning empty strings instead of non-empty
Returning null or empty by mistake
Not checking items before returning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.