13

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

2 Answers 2

30

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

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

hello()
Sign up to request clarification or add additional context in comments.

3 Comments

I add it the line with hello() but I get the error : Use of unresolved identifier 'hello'
Note: The function needs to be declared before its call, like this example is written.
not working Expected '{' in body of function declaration
1

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()

1 Comment

Expected '{' in body of function declaration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.