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

Joseph Maza
Joseph Maza
9,184 Points

Swift closure: Higher order functions challenge

Having difficulty understanding what this challenge wants from me. I don't know what math operation it wants me to do.

Write a function called mathOperation that takes a math operation (as defined in the editor) as the first input, two integers on which we carry out the math operation, and a return type of Int. The body of the function simply returns the result of the math operation you pass in. Note: We are attempting to build a higher order function so your first parameter must be a function. If you're having trouble, read the Function Types as Parameter Types section in the docs

/** 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 }

3 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Joseph Beard-Maza !

Write a function called mathOperation that takes a math operation (as defined in the editor) as the first input

==> func mathOperation(mathOp: (Int, Int) -> Int

, two integers on which we carry out the math operation

==> , a: Int, b: Int)

, and a return type of Int

==> -> Int

The body of the function simply returns the result of the math operation you pass in

==> return mathOp(a, b)

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)
}
Joseph Maza
Joseph Maza
9,184 Points

Thank you this helps a lot!

Christopher Mayfield
Christopher Mayfield
19,928 Points

Holger Liesegang how come you don't need need parentheses around our second return type, like in the differenceBetweenNumbers function above?

thanks

func mathOperation(mathOp: (Int, Int) -> Int, a: Int, b: Int) -> Int 

Can somebody explain this syntax to me. I don't understand it at all.

Karl Metum
Karl Metum
3,447 Points

mathOp: (Int, Int) -> Int specifies that the first parameter passed to the function mathOperation must be a function that takes two Int parameters and returns an Int.