Difference between revisions of "W1351 Swap Function"
Jeff-strong (talk | contribs) m (Editorial review and minor corrections) |
|||
(6 intermediate revisions by 2 users not shown) | |||
Line 5: | Line 5: | ||
A very common need in many algorithms is swapping the values in two variables. It's also very common that these two variables are contained within an array. | A very common need in many algorithms is swapping the values in two variables. It's also very common that these two variables are contained within an array. | ||
== Swapping == | == Swapping == | ||
The first approach that most students take when swapping is | The first approach that most students take when swapping is the following: | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
let x = 5 | let x = 5 | ||
Line 14: | Line 14: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Take the time to think through what you | Take the time to think through what you think will occur when the above code is executed. Then, try it. | ||
{{Observe|Section 1| | {{Observe|Section 1| | ||
Line 22: | Line 22: | ||
The primary issue that most students encounter in this scenario is '''overwriting''' the value of one of the variables, and then assigning this overwritten value to the other | The primary issue that most students encounter in this scenario is '''overwriting''' the value of one of the variables, and then assigning this overwritten value to the other variable. | ||
{{Observe|Section 2| | {{Observe|Section 2| | ||
Line 31: | Line 31: | ||
Watch the following video: [https://www.youtube.com/watch?v=vzCUWAxq6lU Swapping Algorithm] (YouTube) | |||
{{Observe|Section 3| | {{Observe|Section 3| | ||
Line 37: | Line 37: | ||
}} | }} | ||
== Functions | == Functions that Modify Their Arguments == | ||
If we want to modify the value of a (value-type) argument passed to a function, a special syntax is required. | If we want to modify the value of a (value-type) argument passed to a function, a special syntax is required. Consider the following code segment: | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
func increment(n:Int) { | func increment(n:Int) { | ||
Line 54: | Line 54: | ||
Swift protects us from common errors by ensuring that a (value-type) argument passed to a function cannot be altered by a function except under specific circumstances. | Swift protects us from common errors by ensuring that a (value-type) argument passed to a function cannot be altered by a function except under specific circumstances. When (in generally rare cases) this is desirable, a special syntax is required: | ||
# We must ensure that the function declaration specifies that this function is ''enabled'' to modify a specific argument | # We must ensure that the function declaration specifies that this function is ''enabled'' to modify a specific argument | ||
# Whenever we invoke the function, the function invocation must also specify that the specific argument may be modified | # Whenever we invoke the function, the function invocation must also specify that the specific argument may be modified | ||
This strategy ensures that we don't accidentally modify function arguments. | This strategy ensures that we don't accidentally modify function arguments. | ||
An example | An example helps to clarify: | ||
<syntaxhighlight lang="swift"> | <syntaxhighlight lang="swift"> | ||
func increment(n:inout Int) { | func increment(n:inout Int) { | ||
Line 71: | Line 71: | ||
{{Observe|Section 5| | {{Observe|Section 5| | ||
# ''Carefully'' compare the above code segment to the previous segment. | # ''Carefully'' compare the above code segment to the previous segment. What changes are required to modify the value of a (value-type) argument in a function? | ||
}} | |||
{{GoingDeeper| | |||
Technically, the {{SwiftKeyword|inout}} keyword specifies that the argument will be an L-Value. See [W1038 L-Values and R-Values]. | |||
}} | }} | ||
== Exercises == | == Exercises == | ||
{{Exercises| | |||
# {{Assignment|J1351}} Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted. | |||
# {{MMMAssignment|M1351-10}} | |||
}} |
Latest revision as of 16:13, 25 August 2023
Prerequisites[edit]
Introduction[edit]
A very common need in many algorithms is swapping the values in two variables. It's also very common that these two variables are contained within an array.
Swapping[edit]
The first approach that most students take when swapping is the following:
let x = 5
let y = 7
x = y
y = x
Take the time to think through what you think will occur when the above code is executed. Then, try it.
- What syntax errors do you observe in the above code?
- What logic errors do you observe in the above code?
The primary issue that most students encounter in this scenario is overwriting the value of one of the variables, and then assigning this overwritten value to the other variable.
Assuming that the syntax errors in the above code segment are resolved:
- What will be the value of x after the code executes?
- What will be the value of y after the code executes?
Watch the following video: Swapping Algorithm (YouTube)
- What must be done to properly swap the values of two variables?
Functions that Modify Their Arguments[edit]
If we want to modify the value of a (value-type) argument passed to a function, a special syntax is required. Consider the following code segment:
func increment(n:Int) {
n = n + 1
}
var x = 7
increment(n:x)
print(x)
- Will the above code compile correctly? If not, why not?
Swift protects us from common errors by ensuring that a (value-type) argument passed to a function cannot be altered by a function except under specific circumstances. When (in generally rare cases) this is desirable, a special syntax is required:
- We must ensure that the function declaration specifies that this function is enabled to modify a specific argument
- Whenever we invoke the function, the function invocation must also specify that the specific argument may be modified
This strategy ensures that we don't accidentally modify function arguments.
An example helps to clarify:
func increment(n:inout Int) {
n = n + 1
}
var x = 7
increment(n:&x)
print(x)
- Carefully compare the above code segment to the previous segment. What changes are required to modify the value of a (value-type) argument in a function?
Technically, the inout keyword specifies that the argument will be an L-Value. See [W1038 L-Values and R-Values].
Exercises[edit]
- J1351 Create a journal and answer all questions in this experience. Be sure to include all sections of the journal, properly formatted.
- M1351-10 Complete Merlin Mission Manager Mission M1351-10.