Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift 2.0 Functions Function Parameters Function Scope

Viktor Opanasenko
Viktor Opanasenko
1,712 Points

in swift 3 var parameters are deprecated, so how then I can deal with this part - func countDownAndUp(var a: Int) { ?

While coding in playground I've noticed that in this part - func countDownAndUp(var a: Int) { - x-code shows "var parameters are deprecated and will be removed in Swift 3" but if I delete "var" the compiler shows an error. How can I deal with that? Thank you.

Christopher Ford
Christopher Ford
3,407 Points

What happens if you replace 'var' with 'let'?

5 Answers

Viktor Opanasenko
Viktor Opanasenko
1,712 Points

It shows an error because "a" is not mutable when it is "let" and the challenge implies to increment the value of "a".

Christopher Ford
Christopher Ford
3,407 Points

I don't know too much about Swift I'm afraid - looking on Stack Overflow the suggestion (here: http://stackoverflow.com/questions/24077880/swift-make-method-parameter-mutable) seems to be that you should declare the var inside of your function like this:

func countDownAndUp(a:Int) -> Int {
    var a = a
    // etc
}
Viktor Opanasenko
Viktor Opanasenko
1,712 Points

still don't know how to solve it(((((

Kristof Kocsis
Kristof Kocsis
15,455 Points

If you are using Swift 3 for this video at 1:45 in the video your code should look something like this:

func countDownAndUp(a: Int) {

var b = a
var a = a
while b >= 0 {
    b -= 1
    a += 1
    print("a: \(a)")
    print("b: \(b)")
}

}

Rich Braymiller
Rich Braymiller
7,119 Points

still doesn't solve the original issue....

Hello!

I did what you said and it worked for me.

However, I tried to put a variable outside the func countDownAndUp: var a = 20. Then countDownAndUp (a: var a) and I got a few errors. Any idea why?

Thank you very much

Sam S
Sam S
4,447 Points

Christopher's answer above is correct. In Swift 3, you should remove var from the parameter and introduce a local var variable. Apparently this change was introduced because people were confusing var parameters with inout parameters, which differ in scope. More info here: http://stackoverflow.com/questions/36164973/var-parameters-are-deprecated-and-will-be-removed-in-swift-3

Di Wu
PLUS
Di Wu
Courses Plus Student 232 Points

It's always a good practice not to modify parameters. This could reduce side effects :) I guess this is the reason var params was deprecated and finally removed from Swift 3.