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

I don’t understand what i’m doing wrong here.

They asked for

  1. a function called “mathOperation”
  2. Two variables of Int type
  3. a return type of Int
  4. and to return the result of a math function with those integers.

Where did i go wrong

higherOrderFunctions.swift
func mathOperation (a: Int, b: Int) -> (Int) {
  return a + b
}

2 Answers

Read the challenge again:

"Write a function called mathOperation that takes a math operation (as defined in the editor) as the first input, two integers on which we carry out the math operation, and a return type of Int. The body of the function simply returns the result of the math operation you pass in. Note: We are attempting to build a higher order function so your first parameter must be a function. If you're having trouble, read the Function Types as Parameter Types section in the docs"

See that? Your first parameter must be a function. mathOperation should have three parameters. Instead of picking a random math operation to perform on the two integers, you should use the function supplied as the first parameter. An example of this is in the linked documentation if you're having trouble!

Here's an example from the Swift documentation

Hope it helps :)