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 trialGeert Van Campenhout
7,308 PointsSwift 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 ?
2 Answers
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 PointsThey 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
7,308 PointsThanks 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.
Geert Van Campenhout
7,308 PointsGeert Van Campenhout
7,308 PointsIs my code not added in the question? I did check the "add my code to the question" box though...