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

Robin Malhotra
Robin Malhotra
14,883 Points

Working in Xcode, not in challenge.

Here's my code that's working with the simulator.

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

    func mathOperation (operation:(Int,Int)->Int,a:Int,b:Int)->(Int)
    {
        return operation(a,b)
    }
    let difference = mathOperation(differenceBetweenNumbers,a: 4,b: 2)

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello

Simple fix.. take the () off your return.

    func mathOperation (operation:(Int,Int) -> Int, a:Int, b:Int) -> Int {
        return operation(a,b)
    }
Robin Malhotra
Robin Malhotra
14,883 Points

Wait, what? I still don't get how that makes a difference. I mean, differenceBetweenNumbers has brackets on the return type.

Jhoan Arango
Jhoan Arango
14,575 Points

It does not make sense, I know that in Xcode does work, but for some reason on the challenge if you use the () on the return it does not pass the challenge. I knew you had that mistake because I have seen it a million times on other students asking the same question, and they happen to pass the challenge without em. Also I just notice something else that won't let your challenge pass..

Take the parameter names off when calling the function

 let difference = mathOperation(differenceBetweenNumbers, 4,  2)
  • Note: The syntax for swift when doing a return should look like this (Int, Int) -> Int No parenthesis should surround the return type.

Good luck ~