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

Qian Chen
Qian Chen
33,579 Points

My code doesn't pass the challenge. It works in my own playground.

higherOrderFunctions.swift
/** 
  For this code challenge, let’s define a math operation as a function that 
  carries out some work on two integers and returns an integer as well. An 
  example is the function below, `differenceBetweenNumbers`, which takes two 
  integers and calculates the difference between the numbers. After calculating, 
  it returns the difference.
*/

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

// Enter your code below
func mathOperation(f:(Int, Int)->(Int), x:Int, y:Int) -> (Int){
  return f(x, y)
}
let difference = mathOperation(differenceBetweenNumbers, 10, 5)

3 Answers

Try this:

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

let difference = mathOperation(differenceBetweenNumbers, 26, 22)

Let me know if this works.. Its the same thing but with a different func name.

Qian Chen
Qian Chen
33,579 Points

Thanks Aaron, your code works. Is it a bug of the challenge task? Or is there indeed something wrong in my code.

kirkbyo
kirkbyo
15,791 Points

Hey Elgs,

I was having the same problem as you. Try removing the parathesis in between your function parameter return statement.

func mathOperation(f:(Int, Int)-> Int, x:Int, y:Int) -> (Int){
  return f(x, y)
}
let difference = mathOperation(differenceBetweenNumbers, 10, 5)

Hope this helps,

Ozzie

Qian Chen
Qian Chen
33,579 Points

Thanks Kirbyo. I copied and pasted your code into the challenge task, but the second task still doesn't pass. The error message is:

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