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

Geert Van Campenhout
Geert Van Campenhout
7,308 Points

Swift Closures

Stuck on a Swift Closure challenge where we need to input a mathOperation for a Higher Order Function. The editor does not accept my function and I can not see why not. Help is much appreciated. Steve Hunter ?

Geert Van Campenhout
Geert Van Campenhout
7,308 Points

Is my code not added in the question? I did check the "add my code to the question" box though...

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

// Then I get the following error: "X Bummer! Make sure your function is named 'mathOperation' and check your parameters and return type!"

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

They want you to build a function that could theoretically perform any math operation on two integers that returns an integer. We don't know what math operation yet, we're leaving that open ended. You've hard coded that you're going to multiply them, but we don't want to do that.

The function mathOperation should have 3 parameters:

  • a function: one that takes two integers as parameters and returns an integer
  • another integer
  • another integer

It will return an integer.

We don't know what operations will be performed on those two integers yet. We can write this out like this:

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

I've added the underscore in front of the parameter names so we don't have to call the parameters by name when we call the function.

The basic thing to understand is that we're specifying some constraints on what kind of function this will take in. It will be one that takes two Ints in and returns an in. But we don't know any other details of the implementation yet. It could be a function that multiplies, adds, subtracts, or does anything to the two numbers and returns an integer.

Geert Van Campenhout
Geert Van Campenhout
7,308 Points

Thanks a lot for the reply Brendan! I haven't had the chance to continue studying in the last month.. Do you know how to solve the second part of the task?

Using the differenceBetweenNumbers function as an input to mathOperation, compute the difference between any two integers and assign the result to a constant named difference

let difference = differenceBetweenNumbers(Int, Int) mathOperation(difference)

...?

Thank you in advance.