Bird
0
0

Identify the error in this PowerShell function that is supposed to return the sum of two numbers:

medium📝 Debug Q14 of 15
PowerShell - Functions
Identify the error in this PowerShell function that is supposed to return the sum of two numbers:
function Add-Numbers {
  param($a, $b)
  $sum = $a + $b
  Write-Host $sum
}
AThe function does not use the return keyword to send back the sum.
BThe parameters are not declared correctly.
CThe addition operator '+' is invalid in PowerShell.
DThe variable $sum is not assigned properly.
Step-by-Step Solution
Solution:
  1. 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.
  2. 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.
  3. Final Answer:

    The function does not use the return keyword to send back the sum. -> Option A
  4. Quick Check:

    Return keyword needed to send value back [OK]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes