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 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.