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 Functions and Optionals Functions Creating a Function

Kevin McFarland
Kevin McFarland
5,122 Points

Error on calling print( ) in Challenge 1... says println( ) shouldn't be called outside of func... yet is not outside!

Challenge 1 of 2 after the first video on Functions & Optionals is to:

Declare a function named greeting and within that function you will have a println statement that outputs “Hello".

Here is my code:

func greeting( ) {

println("Hello")

}

greeting( )

I get the response: “Bummer! You shouldn't call println directly. It needs to be in the body of the function.”

Yet the exact code in Xcode yields the proper results with no errors!

I don’t understand what I’m doing wrong! How is println( ) not being called within the function?

Please help!

Thanks,

Kevin

3 Answers

func greeting() {
      println("Hello")
}

You don't need to add greeting() after the second curly brace.

Kevin McFarland
Kevin McFarland
5,122 Points

Thanks Kyle,

I realized that the error stemmed from the fact that I was going a step beyond what

the Quiz had requested... it simply stated:

"Declare a function named greeting and within that function you will have a println statement that outputs “Hello"."

I took it a step further by calling the function, which it had not requested.

I'm paying closer attention to what the Quiz is actually requesting!

Thanks,

Kevin

David Maybach
David Maybach
2,092 Points

Made the same mistake and included greeting ( ) outside the curly bracket.

I guess, Greeting wasn't needed to be executed in this challenge because there was nothing to be executed other than the passing a parameter Hello.