Bird
0
0

What is wrong with this PowerShell function parameter declaration?

medium📝 Debug Q6 of 15
PowerShell - Functions
What is wrong with this PowerShell function parameter declaration?
function Sample { param([string]$text =) Write-Output $text }
AThe default value for the parameter is missing after the equals sign.
BThe parameter type [string] is not allowed with default values.
CThe function is missing the return statement.
DParameters cannot be declared inside the param block.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the parameter declaration

    The parameter is declared as [string]$text = which ends with an equals sign but no value.
  2. Step 2: Understand default value syntax

    In PowerShell, a default value must be assigned after the equals sign, e.g., $text = 'default'. Leaving it empty causes a syntax error.
  3. Final Answer:

    The default value for the parameter is missing after the equals sign. -> Option A
  4. Quick Check:

    Check if default value is assigned after '=' [OK]
Quick Trick: Default values must follow '=' with a valid value [OK]
Common Mistakes:
  • Leaving '=' without a value
  • Assuming parameter type disallows defaults
  • Forgetting to assign a default value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes