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 trialAldrin Vicente
2,747 PointsHigherOrderFunctions(mathOperation)
I have no idea what I'm doing wrong and I'm not able to find a helpful answer on the forum. My 'error' in the treehouse console keeps telling me to make sure I named the function "mathOperation" and to check my parameters.
/**
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(operation: (Int, Int) -> Int, a: Int, b: Int {
func operation(a: Int, b: Int) -> Int {
return a + b
}
}
1 Answer
Jhoan Arango
14,575 PointsHello Aldrin: 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.
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, 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 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.
Aldrin Vicente
2,747 PointsAldrin Vicente
2,747 PointsFantastic! Thanks Jhoan it's very much appreciated. Besides the Swift iBook that Apple offers, are there any physical books I can add to my library(non-electronic)?