Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Closures Functions as First Class Citizens Higher Order Functions

Paul Je
Paul Je
4,435 Points

Copied it right off of the doc, but to no avail

Can't seem to get it right, I just noticed the line in the document on Apple's site. But still not working.. Any help would help a lot lol

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(differenceBetweenNumbers: (Int, Int) -> Int, _ a: Int, _ b: Int) {
    print(differenceBetweenNumbers(a, b))
}

mathOperation(differenceBetweenNumbers, 5, 3)

2 Answers

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You're nearly there! You just need to return the result of applying differenceBetweenNumbers, rather than printing it.

func mathOperation(differenceBetweenNumbers: (Int, Int) -> Int, _ a: Int, _ b: Int) { // return an Int value
    print(differenceBetweenNumbers(a, b)) // return this value rather than printing it
}

I hope this helps!

Anjali, did you try your code in the challenge?

When I run your code I get: Bummer! Make sure your function is named mathOperation and check your parameters and return type!

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

I didn't post working code. I posted Paul's code with instructions in comments on how to turn it into functional code. If you follow those instructions (returning an Int value in the function header, and returning the value in the print statement rather than printing that value), the resulting code should work in the challenge.

Paul Je
Paul Je
4,435 Points

Okay just followed Anjali's suggestion and got the answer! This one was weird to format but finally got it haha