This is my code:
func removeFromString( _ s: inout String, Character c:Character) -> Int {
var s1 = s
var nRemoved = 0
while let ix = s1.characters.index(of: c) {
s1.removeSubrange(ix...ix)
nRemoved += 1
}
return nRemoved
}
var s2 = "everest"
let resb = removeFromString(&s2, Character: Character("e"))
My problem is this is supposed to change s2 to "vrst" after using the inout
parameter s
. But it's not modifying the var s2
at all. What wrong I'm I doing? Or I'm expecting a wrong output? Do help. Thanks.
Swift inout parameter is a parameter that can be changed inside the function where it's passed into. To accept inout parameters, use the inout keyword in front of an argument. To pass a variable as an inout parameter, use the & operator in front of the parameter.
An InOut variable in a function header is for a variable that may be written as well as read within the function, but may also be written, as well as read outside the function.
Procedure with IN-OUT parameter: An INOUT parameter is a combination of IN and OUT parameters. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.
A function can have OUT or IN OUT parameters, but this is bad coding practice. A function should have a return value and no out parameter. If you need more than one value from a function you should use a procedure.
An inout parameter is a value that is supplied into a function, updated by the function, and then returned out to replace the original value. What does _: mean in Swift? The underscore operator (_) in Swift denotes an unnamed argument or label. What is a good use case for an inout parameter in Swift?
A good use case will be swap function that it will modify the passed-in parameters. Swift 3+ Note: Starting in Swift 3, the inout keyword must come after the colon and before the type. For example, Swift 3+ now requires func changeChar (char: inout Character). Show activity on this post.
Claim Discount In this tutorial, we will learn about function parameters and return values in Swift with the help of examples. A function parameter is a value that is accepted by a function. Before you learn about function parameters and return values, make sure to know about Swift functions.
An inout parameter is a special type of parameter that can be modified inside a function and the changes apply outside the function. Here is an illustration of a function that takes a number A as an inout parameter and modifies it: To turn a function parameter into an inout variable that can be changed inside a function:
By moving s
into s1
you are making a copy of it since String is a struct, and all structs are pass by value. So in the end you're only changing s1
and not the passed in string, s
. Just remove it:
func removeFromString( _ s: inout String, Character c:Character) -> Int {
var nRemoved = 0
while let ix = s.characters.index(of: c) {
s.removeSubrange(ix...ix)
nRemoved += 1
}
return nRemoved
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With