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 trialJesus Cordoba
1,414 PointsFunction Type as Parameter Type
/* I need to take a math operation (as already defined), 2 Int on which the code carry out to match the Operation, and return type Int. The body should only return the result of the math operation I pass. And after some hours, i got confused... this is ´Closure this´is crazy */
/**
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 mathOperations(differenceBetweenNumbers: (Int, Int)) -> Int {
return mathOperations((5, 6))
}
4 Answers
Jesus Cordoba
1,414 PointsIt turns out that my code was missing the letters. But is quite confusing. I let you this link which helped me a lot.
Jesus Cordoba
1,414 PointsYou are 100% right Jhoan Arango I tried in Xcode and it pop up the error you mention. Weird. I guess the challenge has a bug. On the other hand I actually was trying/Playing with this ´parameters´name and makes things easier to understand while reading code.
// Self Examples
func nombresDe(primerNombre nombre: String, seundoNombre nombreDos: String ) -> String {
return ("\(nombre) \(nombreDos)")
}
let parteUno = nombresDe(primerNombre: "Jesus", seundoNombre: "Algo")
func apellidosDe(apellidoUno paterno: String, apellidoMaterno materno: String ) -> String {
return("\(paterno), \(materno)")
}
let parteDos = apellidosDe(apellidoUno: "Cordoba", apellidoMaterno: "OtroAlgo")
print("Tu Nombre completo es: \(parteUno) \(parteDos)")
Jhoan Arango
14,575 PointsIt's not a bug, I am assuming that the compiler is using swift 1.2 which had different rules when it comes to naming parameters.
But good job on understanding it now.
De que parte de Mexico eres ?
Jesus Cordoba
1,414 PointsJhoan Arango soy de Cancun! - Pero viajo mucho al D.F - Veo que `Quieres crear un app to take over the world´... Y estoy por el mismo canal. Por ello comencé a tomar estos cursos. ¿Te interesaría platicar? jesus.cordoba@icloud.com -
Jesus Cordoba
1,414 PointsJesus Cordoba
1,414 PointsThanks Jhoan, i appreciate such a quick response. One last question - Why would the task challenge only accept in the second step by calling the function as this:
let difference = mathOperation(differenceBetweenNumbers, 5, 5)
which while comparing yours is a bit different. Yours included the letter of the variables
var someVariable = mathOperation(differenceBetweenNumbers, a: 10, b: 5)
Is it just syntax's.?
Thanks - Regards from Mexico.
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsThat's a good question Jesus.
If you notice on the declaration of the function
the second, and third parameter have been named a and b. Since they also use their local parameter name as the External parameter name, then you need to use them when calling the function.
If you want to learn more about parameter names, I also have an answer to that. Which I just gave it to someone a few days ago. You can see it here
If you have any other questions please let me know.
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsBy the way Jesus Cordoba,
For some reason, the challenge does pass with the first option let difference = mathOperation(differenceBetweenNumbers, 5, 5), and not with the one I gave you, with the parameter names.
BUT to make it clear, the correct way to do it, is the way I showed you. When you give your parameters external names, you have to use them.
If you try it on Xcode, you will see that if you don't use the parameter names when calling the function, it will give you an error.