0
0
Swiftprogramming~10 mins

In-out parameters for mutation in Swift - Interactive Code Practice

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

Complete the code to declare an in-out parameter in the function.

Swift
func increment(number: [1] Int) {
    number += 1
}
Drag options to blanks, or click blank then click option'
Alet
Bvar
Cinout
Dmutating
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' instead of 'inout' to declare a mutable parameter.
Forgetting to use 'inout' when intending to modify the parameter.
2fill in blank
medium

Complete the code to call the function with an in-out parameter correctly.

Swift
var value = 5
increment([1]value)
Drag options to blanks, or click blank then click option'
A#
B*
C$
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the '&' symbol when passing the variable.
Using incorrect symbols like '*', '$', or '#'.
3fill in blank
hard

Fix the error in the function call by completing the blank.

Swift
func doubleValue(number: inout Int) {
    number *= 2
}

var num = 10
doubleValue(number: [1]num)
Drag options to blanks, or click blank then click option'
A&
B#
C$
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the function without the '&' symbol.
Using other symbols like '*', '$', or '#'.
4fill in blank
hard

Fill both blanks to create a function that swaps two integers using in-out parameters.

Swift
func swapInts(a: [1] Int, b: [2] Int) {
    let temp = a
    a = b
    b = temp
}
Drag options to blanks, or click blank then click option'
Ainout
Bvar
Clet
Dmutating
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' or 'let' instead of 'inout'.
Declaring only one parameter as 'inout'.
5fill in blank
hard

Fill all three blanks to create a function that increments a number by a given amount using in-out parameters.

Swift
func incrementBy(amount: [1] Int, number: [2] Int) {
    number [3]= amount
}
Drag options to blanks, or click blank then click option'
Alet
Binout
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Declaring amount as inout instead of let.
Using '-=' instead of '+='.
Not using inout for number.