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

Amit Kalra
Amit Kalra
3,078 Points

Am I missing something in this function?

I'm trying this challenge but it keeps saying make sure I have a function named mathOperation and the return type is type Int. I did to that, am I right?

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

return (a + b)

}

3 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Amit:

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.

I'm using closures for very long, but this challenge was completely not clear.

After wroting correct answer I still couldn't complete it, which was confusing, misleading and frustrating. I only used more parenthesis to make it more readable.

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