Binary operator '+' cannot be applied to operands of type '()' and 'String' error

Hi. I'm getting an error on the index 1 of my code : Binary operator '+' cannot be applied to operands of type '()' and 'String'. The code block is the following

func addDance (_ sentence: String) {

    return addDance(sentence: String) + " and then we dance"

}

The instruction of the exercise I'm doing is the following : «Create a function addDance that takes a string, appends a phrase about dancing (like "and then we dance!" or "but no dancing", according to your taste), and returns the new string.

 Call the addDance function passing in myPlans, and assign the result to friendPlans. »

Accepted Reply

func addDance (_ sentence: String) {
    return addDance(sentence: String) + " and then we dance"
}

what @robnotyou said, plus.

There are several errors here:

  • when you call a func, you need to pass a parameter, not its type
  • so it should be
return addDance(sentence) + " and then we dance"
  • But, calling the func inside itself will cause infinite recursion:
    • you call addDance
    • this will call addDance
    • which will call again addDance
    • and this ad libitum, until crash

.

  • the error means that

addDance(sentence: String) is considered as a func (which would be different from addDance (_ sentence: String) as it has not the exact same signature : _ is missing) with no return type (that's what () means)

  • Thanks for the clarification @Claude31. Not gonna lie, coding has many things to think about to avoid making mistakes 😭

  • For sure. But we all learn from mistakes. Good continuation and don't forget to close the thread by marking the correct answer.

Add a Comment

Replies

Perhaps you mean this?

func addDance(_ sentence: String) -> String {
    return sentence + " and then we dance"
}
func addDance (_ sentence: String) {
    return addDance(sentence: String) + " and then we dance"
}

what @robnotyou said, plus.

There are several errors here:

  • when you call a func, you need to pass a parameter, not its type
  • so it should be
return addDance(sentence) + " and then we dance"
  • But, calling the func inside itself will cause infinite recursion:
    • you call addDance
    • this will call addDance
    • which will call again addDance
    • and this ad libitum, until crash

.

  • the error means that

addDance(sentence: String) is considered as a func (which would be different from addDance (_ sentence: String) as it has not the exact same signature : _ is missing) with no return type (that's what () means)

  • Thanks for the clarification @Claude31. Not gonna lie, coding has many things to think about to avoid making mistakes 😭

  • For sure. But we all learn from mistakes. Good continuation and don't forget to close the thread by marking the correct answer.

Add a Comment

// Define and call your function here:

func addDance (_sentence: String) {

    return addDance (sentence) + " and then we go dance"

} ***getting cannot find "sentence" in scope please help. I followed what you helped him out with and still getting the same error