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 2.0 Functions Functions in Swift 2.0 Basic Function Syntax

Functions

Wonder why my code isn't working?

Challenge Task 1 of 1

Let's get in some practice writing a really basic function to make sure we know our syntax.

Declare a function named greeting. In the body of the function, simply add a print statement that prints the string "Hello" /Users/mattconway/Desktop/Screen Shot 2016-01-16 at 11.34.38 AM.png

functions.swift
// Enter your code below
func calculateGreeting() {
let greeting = "Hello"
print(greeting)
}
calculateGreeting()

3 Answers

Hi Matt,

The compiler is looking for a function named greeting - the tests behind the challenge will be calling a function named greeting so your code is not giving the output it expects:

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

With the above code, calling greeting() will generate the output Hello. With your code, calling greeting() will not. The instructions are Declare a function named greeting. In the body of the function, simply add a print statement that prints the string "Hello", so the challenge is expecting to be able to call a function that is precisely named.

I hope that makes sense.

Steve.

func greeting(){ print("Hie") }

Hi Patrick,

This thread is from a course that has been retired since January 2017 as the Swift language develops so quickly. If you're struggling with one of the newer Swift courses; best start a new thread with a link to the challenge. Your code looks OK but the output expectation might be very specific, i.e. it may expect the string Hello to be printed, rather than any other string.

Steve.

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

greeting()