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

Radu Savutiu
Radu Savutiu
3,167 Points

High order functions Challenge 2 of 2 broken

My code is attached and works fine on my Xcode. This basically means there is something wrong in the Treehouse testcase. I deduce this by the fact that there is no compiler warning (meaning it compiles properly?)

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
}

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

let difference = mathOperation(differenceBetweenNumbers, 5, 2)



// Enter your code below

2 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Radu:

Nope is not broken. Your code is missing an essential part. And that’s your named parameters. You are calling your function without them.

// use this instead
let difference = mathOperation(differenceBetweenNumbers, a: 5, b: 2)

If you wanted to omit the names so that you didn’t have to use them when calling the function you would have to do something like this.

func mathOperation(operation: (Int, Int) -> Int, _ a: Int, _ b: Int) -> (Int){
  return operation(a, b)
}
Radu Savutiu
Radu Savutiu
3,167 Points

Hi Jhoan! Unfortunately your solution does not compile at all. The compiler says ("extraneous arguments....").

PS: I also tried that solution myself.

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Radu:

You are right, I apologize. Apparently I am used to working with Swift 2.0 which the first parameter omits it’s external name, and the subsequent parameters don’t by default. In order for you to omit them you have to use the _ .

Try this code, I just did the challenge with it and it worked. I am not exactly sure why yours won’t work other than your return is -> (Int) and mine is -> Int. But I don’t see why that would be a problem.

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

// Enter your code below

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

let difference = mathOperation(differenceBetweenNumbers, 10, 5)
Radu Savutiu
Radu Savutiu
3,167 Points

Great Jhoan! This was the problem, returning (Int). Your code works great and I solved it now!

Many thanks!

PS: I see you're a hobby programmer. Try Android at some point, it's great!