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

Anthony MacMahon
Anthony MacMahon
2,213 Points

The Higher Order Functions assignment is not recognizing my answer as correct, when XCode Playground does

I've entered the following code into the assignment, which calculates properly in Xcode. However, I keep getting a bummer message. As far as I can understand, everything is entered correctly, and I'm getting a correctly returned answer in my playground, but it's not evaluating. Can anyone help? Does it have something to do with the fact that I'm returning a - b in both differenceBetweenIntegers and mathOperation?

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

// Enter your code below

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

let difference = mathOperation(differenceBetweenNumbers, a: 4, b: 3)

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

I solved it. But I'm not 100% why I solved it. I think it has to do with:

1) Your (and my) Xcode is now operating Swift 2.0. This course is not. There is a Swift 2.0 track, but it doesn't have a section on closures yet.

2) Something changed in Swift 2.0 with regards to whether you need to call parameters by name.

But anyway, I solved it by going back into the functions we defined in the previous steps and putting the underscore before all the parameters after the first one, so that I don't have to call them by name:

func differenceBetweenNumbers(a: Int, _ b:Int) -> (Int) {      // now an underscore before b: 
    return a - b
}

func mathOperation(mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) -> Int {     // now underscores before a and b
    return mathFunction(a, b)
}

let difference = mathOperation(differenceBetweenNumbers, 3, 4)

P.S. Looking again at your code, there's something wrong with the implementation of your mathOperation function. You're just calculating the difference between numbers explicitly just the same as in differenceBetweenNumbers. You need to run the two arguments of mathOperation through the mathFunction - which is the name of the function parameter we're defining. This placeholder function could theoretically could do anything to the two numbers. It will calculate the difference only later because we pass in the differenceBetweenNumbers function as an argument.

Anthony MacMahon
Anthony MacMahon
2,213 Points

Thanks Brendan!

Yeah, I keep running into weird issues about naming variables, or leaving them unnamed with the _ command.

And yeah, I realize I was calling the result incorrectly now. I knew it was wrong, but couldn't figure out how to call a function result as a return.