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

Vaclav Mlejnsky
Vaclav Mlejnsky
6,832 Points

Computing difference between numbers with function as a parameter

I am suppose to do this: 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.

I don't understand why I keep getting this error: Make sure you are computing the difference between two integers using the functions created and assigning the result to a constant named difference

But in playground in Xcode everything works just fine.

higherOrderFunctions.swift
/** 
  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(someFunc:  (Int, Int) -> Int, a: Int, b: Int) ->  (Int) {
    return  someFunc(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, 8, 1)

4 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Yes except the numbers, I wasn't sure if there was a small typo I didn't see. You might need to change the function:

func mathOperation(anyAddFunc: (Int, Int) -> Int, a: Int, b: Int) -> Int{
    return anyAddFunc(a,b)

It looks the same to me, but maybe it's not.

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Try copying and pasting this code, let me know if it works.

let difference = mathOperation(differenceBetweenNumbers, 2, 1)
Vaclav Mlejnsky
Vaclav Mlejnsky
6,832 Points

It doesn't work. Isn't it same as last line of my code?

Corey Dutson
PLUS
Corey Dutson
Courses Plus Student 3,193 Points

Glad I found this, because I couldn't figure out why I was getting my answer rejected. for the record, my function signature (which passed step one) was:

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

This threw an error on the second part, and I had to change it to:

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

Note that I had to remove the () off the first Int return. While I understand why now, it shouldn't have passed the first stage, because that was way more confusing to me as a result.