Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a function named 'Greet' that prints 'Hello'.
PowerShell
function [1] { Write-Output "Hello" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'Greet'.
Missing the function keyword.
✗ Incorrect
The function keyword is followed by the function name. Here, the function is named 'Greet'.
2fill in blank
mediumComplete the code to define a function named 'Add' that takes two parameters.
PowerShell
function Add([1]) {
$sum = $a + $b
Write-Output $sum
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names different from those used inside the function.
Forgetting to separate parameters with commas.
✗ Incorrect
The function parameters are listed inside parentheses separated by commas. Here, '$a' and '$b' are used in the function body, so they must be the parameters.
3fill in blank
hardFix the error in the function definition to correctly define a function named 'Multiply' that returns the product of two numbers.
PowerShell
function Multiply {
param([1])
return $x * $y
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the variables used inside the function.
Missing the param block.
✗ Incorrect
The param block defines the parameters. Since the function uses $x and $y inside, the param block must declare $x and $y.
4fill in blank
hardFill both blanks to define a function 'Square' that takes one parameter and returns its square.
PowerShell
function Square {
param([1])
return [2] * [1]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in param and return statement.
Forgetting to multiply the parameter by itself.
✗ Incorrect
The parameter is named '$num'. The return statement multiplies '$num' by itself to get the square.
5fill in blank
hardFill all three blanks to define a function 'GreetPerson' that takes a name and prints a greeting.
PowerShell
function GreetPerson {
param([1])
$message = "Hello, [2]!"
Write-Output [3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to print the message variable.
✗ Incorrect
The parameter is '$name'. The message uses '$name' to create the greeting. The output prints the '$message'.