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

I'm stuck at this one.

I don't know how to complete this task.

The challenge is this: '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'

Please HELP!

Thanks in advance.

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

6 Answers

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Hammad: I will suggest to look into Function Types, it will explain it better than what I can.

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

But I will give it a try :)

So the challenge wanted us to create a higher order function, so they gave you the first function as an example. The function you have to create is one that takes another function of the same TYPE (of the example function), 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.

Jhoan Arango
Jhoan Arango
14,575 Points

Hey Hammad, this challenge was a bit hard for me too when I did it, I had to go into the documents to fully understand since I think what the challenge ask here was not shown in the video..

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

// Enter your code below

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

}

But what does this code do:

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

I mean I haven't seen this before.

Please describe this.

Thanks!

Hey Jhoan,

Great answer it helped a lot. One more question though: do you have to declare the function you use as a parameter before?

Best,

Antoine

a function can get use as a parameter. though does this function you use as a parameter needs to have been define before you use it as a parameter or can you just create a new function to use as a parameter directly in the other function? I hope my question is clearer now.

Many thanks in advance for your help.

Best,

Antoine

Jhoan Arango
Jhoan Arango
14,575 Points

No, you do not have to define the function before. And in fact you can use any other function that may do any other type of calculation and use it as argument. As long as the function has the required type, it will work.

Thanks a lot Jhoan