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

Will Mead
Will Mead
1,376 Points

Task One on Swift Closures, Please Help

Please can you help me with this, I don't know what's wrong with my code

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

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Will:

This was the same answer I gave to someone else in the Forum, I copied and pasted on here for the purpose of having more information on the forums so that other people can find it. Also I suggest reading the book on swift from apple, for this challenge.

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2 Prerelease).” iBooks. https://itun.es/us/k5SW7.l

Here is my explanation.

  • So the challenge wanted us to create a higher order function. The function you have to create is one that takes another function of the same TYPE ( as the one given as an example ) as an argument in its parameter.. Think of it as like any other type in swift. Int, Bool, Double, String etc.
func functionType(a: Int) -> Int {
// Some code
}

// The type of this function is (Int) -> Int
  • Now, that we know what is a function type, let’s go into our challenge code and see the type for that one.
func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
  return a - b
}

// The type of “differenceBetweenNumbers" is (Int, Int) - > Int.

/*
- Now that we have a TYPE, we want to create a function that takes in
 this TYPE of function as an argument in its parameter. 
*/
func mathOperation(anyAddFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int{
    return anyAddFunc(a,b)

// Calling the function

var someVariable = mathOperation(differenceBetweenNumbers, a: 10, b: 5)

// This will give you the result of 5


}
  • Notice how the first parameter for mathOperation is of the same type (Int,Int) -> Int and is named anyAddFunc, the second and third parameters which are named a, and b are of type Int. These are used as the two input values for the provided differenceBetweenNumbers function.

  • So in conclusion, we created a function that takes in as an argument in its parameter another function of the same type. And at the same time we passed it values so that it can do its calculation.

  • Do read the book, it does explain very well.

Hope that helps, let me know if there something you don’t understand.