0
0
PowerShellscripting~10 mins

Function definition in PowerShell - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
APrintHello
BSayHello
CGreet
DHelloFunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than 'Greet'.
Missing the function keyword.
2fill in blank
medium

Complete 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'
A$a, $b
B$x, $y
C$num1, $num2
D$first, $second
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names different from those used inside the function.
Forgetting to separate parameters with commas.
3fill in blank
hard

Fix 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'
A$a, $b
B$num1, $num2
C$first, $second
D$x, $y
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the variables used inside the function.
Missing the param block.
4fill in blank
hard

Fill 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'
A$num
C$value
D$input
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in param and return statement.
Forgetting to multiply the parameter by itself.
5fill in blank
hard

Fill 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'
A$name
C$message
D$person
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Forgetting to print the message variable.