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 Functions in Swift Adding Power to Functions Function Parameters

Daniel Schlichter
Daniel Schlichter
783 Points

Whats wrong with my code?

I have no idea what could be wrong with my code.. can anybody help :/ ?

functions.swift
// Enter your code below
func getRemainder (value a  : Int,divisor b  : Int) -> Int {
    let modulo = a % b
    return  modulo }

    let result = getRemainder(a: 10, b: 3)

1 Answer

Hi Daniel,

You probably need to use the external names you created for each parameter. Also, while it isn't incorrect and won't fail the challenge, you can omit the constant that you immediately return from the function - just return the result of the expression directly:

func getRemainder(value a: Int, divisor b: Int) -> Int{
  return a % b
}

let result = getRemainder(value: 10, divisor: 3)

I hope that helps,

Steve.

If you click the 'Preview' button with your code, it'll point you in the direction of your error like this:

swift_lint.swift:11:26: error: incorrect argument labels in call (have 'a:b:', expected 'value:divisor:')
let result = getRemainder(a: 10, b: 3)
                         ^~      ~
                          value  divisor
Daniel Schlichter
Daniel Schlichter
783 Points

thanks it works but its a bit strange because the same code with the exactly same function doesn't works at the playground

The Playground has no idea what you are asked to achieve in the challenge. The tests behind the challenge compiler are specifically looking for adherence to the question as set; it is expecting you to use the argument labels. The Playground doesn't know that. The Playground will run code that is syntactically correct but which will still fail the challenge.

By way of example, this "passes" the challenge by returning the result expected for the challenge example but is clearly wrong despite result holding the correct value that the challenge is expecting. This works fine in the Playground as it is syntactically correct but, clearly, should not pass for Treehouse's purposes.

func getRemainder(value a: Int, divisor b: Int) -> Int{
  return 1
}

let result = getRemainder(value: 10, divisor: 3)

That said, in my Playground I get the same error.

Imgur