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 Swift Closures Functions as First Class Citizens Higher Order Functions

kirkbyo
kirkbyo
15,791 Points

Trouble computing the difference between two integers.

I am having trouble locating where I went wrong with task 2 of this challenge.

Questions:

Using the differenceBetweenNumbers function as an input to mathOperation, compute the difference between any two integers and assign the result to a constant named difference.

Error:

Bummer! Make sure you are computing the difference between two integers using the functions created and assigning the result to a constant named difference.

My Code:

func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
    return a - b
}

func mathOperation(mathFunc: (Int, Int) -> (Int), a: Int, b: Int) -> Int {
    return mathFunc(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, 20, 10)

Ozzie

1 Answer

Your code looks fine. This worked for me. I used someOperationFunc(a,b)

func mathOperation(someOperationFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int {
    return someOperationFunc(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, 26, 22)
kirkbyo
kirkbyo
15,791 Points

The error was caused by having the function parameter return being wrapped in parathesis.

// Does Not Work in the challenge. 
func mathOperation(mathFunc: (Int, Int) -> (Int), a: Int, b: Int) -> Int {
    return mathFunc(a, b)
}

// Works
func mathOperation(mathFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int {
    return mathFunc(a, b)
}

Anyways, thanks for your help!

Ozzie

What is the difference between Int and (Int)?

Why isn't it:

let difference = mathOperation(differenceBetweenNumbers() , 26, 22)

Is the function a constant being passed in?