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

does not work

the code runs in Xcode, but does not get accepted

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(operation: (Int, Int) -> Int, a: Int, b: Int) -> (Int) {
  return operation(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, a: 10, b: 5)

3 Answers

Hey Hermann Blum,

You don't need the external argument names a: and b: when you call your mathOperation function. It should just be called as:

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

// Enter your code below
func mathOperation(mathOp: (Int, Int) -> (Int), _ a: Int, _ b: Int) -> Int {
    return mathOp(a, b)
}
// We can pass in any two integer values that we want to evaluate
let difference = mathOperation(differenceBetweenNumbers, 20, 10 )

If you want to refer to this post, I posted a more detailed response to this code challenge. Check it out!

https://teamtreehouse.com/community/stuck-on-this-swift-closures-task-with-my-higher-order-function.

Good Luck!

That is true, but it does not make

let difference = mathOperation(differenceBetweenNumbers, a: 10, b: 5)

wrong. For your information, in the first place I had the code like you suggested, the Challenge still said it was not working. So in my opinion something with this challenge environment is just broken.

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

I updated my solution to include the underscores _ to omit the external parameter name.

ahhh I see, OK totally forgot about this, Thanks a lot