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 trialrandal beck
2,723 PointsI'm having trouble with the calling a function with a function argument. I have reviewed the documentation for swift.
This is for the test at the end of swift closures. I have tried everything I know but it doesn't work.
/**
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(mathFunc: (a: Int, b: Int) -> Int ) {
return mathFunc(a, b)
}
1 Answer
Christopher Augg
21,223 PointsHello Randal,
This can be a tough one to understand for sure. I will go through it step by step. The first part of the instructions says, "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."
We are given the following code to start with:
func differenceBetweenTwoNumbers(a : Int, b : Int) -> Int {
return a - b
}
This function takes two integers and returns the difference of a - b as an Int.
So we need to write a function named mathOperation that takes 3 parameters.
- The first parameter for mathOperation is a function that takes the same parameters as the math operation function named differenceBetweenTwoNumbers (Two Ints) as well as the same return type (an Int). The name of this parameter function is arbitrary so we can just call it calculate.
func mathOperation(calculate : (Int, Int) -> Int, secondParam, thirdParam) -> returnType {}
- The second and third parameters are integers labeled a and b respectively.
func mathOperation(calculate : (Int, Int) -> Int, a: Int, b: Int) -> returnType {}
- And this method should also return an Int. It does so by returning a function call on the function passed into mathOperation along with the required integers.
func mathOperation( calculate : (Int, Int) -> Int, a: Int, b: Int ) -> Int {
return calculate(a,b)
}
- And now we can call the math operation method, pass in the difference function along with two integers and it will return the difference to us as a constant.
let difference = mathOperation(differenceBetweenTwoNumbers, 50, 10)
func differenceBetweenTwoNumbers(a : Int, b : Int) -> Int {
return a - b
}
func mathOperation( calculate : (Int, Int) -> Int, a: Int, b: Int ) -> Int {
return calculate(a,b)
}
let difference = mathOperation(differenceBetweenTwoNumbers, 50, 10)
I hope this helps.
Regards,
Chris
randal beck
2,723 Pointsrandal beck
2,723 PointsThanks I got it!