Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Closures in Swift 2 First Class Functions Functions as First Class Citizens

I'm having difficulty with the challenge: Write a function named addTwo, task 1 of 2

I am having trouble with the following question: Write a function named addTwo that accepts an Int, adds the value 2 to it and returns the resulting integer. Note: This task doesn't require that you call the function yet.

I receive the error: "Make sure your function is named addTwo, accepts an Int, and returns an Int.". Here is the code I am using:

functions.swift
// Enter your code below
func addTwo(a: Int, b: Int) -> Int {
    return a + b
}

4 Answers

Never mind, it just took me a 2nd(or 8th) try.

func addTwo(a: Int) -> Int {
    return a + 2
}

Here is the rest of it for task 2 of 2 if anyone needs it:

/*
let addition = addTwo
let result = addition(5)
*/
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Well the good news is that your code is functional. But challenges are super picky and you've written code that adds two numbers together. Which isn't a bad thing! But what the challenge wants is to be able to pass in any integer and the function will add two to it. So if we were to pass in a 9 ... it'll return an 11. If we pass in a 40... it'll return a 42. So give it another try with a new understanding of the instructions :thumbsup:

Eric Hodgins
Eric Hodgins
29,207 Points

Hi Arthur, you should just pass in 1 Int value. Not two. With that Int value add 2 to it.

Something like: return a + 2

Aananya Vyas
Aananya Vyas
20,157 Points

we would need an external argument 'to' to be added too for the challenge to pass as the challenges tend to be really picky so ...

func addTwo(to a: Int) -> Int {
    return a + 2
}

something like this... :)