user2924482
user2924482

Reputation: 9140

Xcode playground not executing functions

I created a new playground and I add simple function but the function is never been call:

var str = "New playground"

func hello()
{
    print("Hello, World")
}

enter image description here

Any of you knows why the function is not been call ?

I'll really appreciate your help

Upvotes: 13

Views: 8965

Answers (2)

Naishta
Naishta

Reputation: 12383

Also, If you are calling the method before its implementation, the Swift Playground wouldn't allow you to do that, and errors out as below

Use of unresolved identifier 'hello'

WRONG ORDER:

hello()
func hello()
{
    print("Hello, World")
}

RIGHT ORDER

func hello()
{
    print("Hello, World")
}
hello()

Upvotes: 1

Arsen
Arsen

Reputation: 10961

Because you didn't call the function. Just call it:

func hello()
{
    print("Hello, World")
}

hello()

Upvotes: 30

Related Questions