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

This works in Xcode Playground but not in the Treehouse editor? No error codes returned either.

Just take a look at my code and tell me what I'm doing wrong. In an Xcode playground I get a return of 10, in Treehouse, it's just wrong and I'm unsure why.

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(mathOpFunc: (Int, Int) -> (Int), a: Int, b: Int) -> Int {
    return mathOpFunc(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, 20, 10)
Mathieu HAYS
Mathieu HAYS
9,623 Points

I've got the same issue on my side. Probably an issue from Treehouse

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey guys!

In this case the code check has it's own idea of how the code has to be formatted :) In short, although your code is correct, you have to remove parenthesis from the return type of both the function you pass in and the function itself. Been there before :)

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

let difference = mathOperation(differenceBetweenNumbers, 2, 1)

You can find the original answer here

Thanks for the help, that's appreciated, but this needs to be fixed. This shouldn't be failing. It's leading students to believe they're wrong, when in fact they're right.

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

Agreed! I think this already has been forwarded to the staff, but apparently it has not changed yet...