Apple's Own Code In It's Documentation Has Errors?

Hi. When tested in Playfround, Apple's own code, from it's own documentation (from Swift 4.2 & 5 book), seems to produce errors.

How can it be corrected, if it's an invalid code (that got published somehow):


func greetAgain(person: String) -> String {
    return "Hello again, " + person + "!"
}
print(greetAgain(person: "Anna"))


func greet(person: String, alreadyGreeted: Bool) -> String {
    if alreadyGreeted {
        return greetAgain(person: person)
    } else {
        return greet(person: person)
    }
}
print(greet(person: "Tim", alreadyGreeted: true))


Also, how can the this block of code (or is this called a code snippet? What's the difference, by the way?) be fixed, if the arguments/input value, 'alreadyGreeted' is changed to 'false' in line 14?


Thank you.

God bless, Proverbs 31

Advanced happy Easter ✟🐰 (see you all in heaven & the heaven of all programming languages: Swift:-)

Replies

I'm afraid you might be missing the fact that the part you are referring to is written in the context where this function is defined:

func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

(See. Defining and Calling Functions)


Documentations (or books) of Apple may have some errors and you can send a bug report using the link Report Bugs shown below.

But you should better check if something is really an error before reporting it as error.

I does work perfectly.


Of course, you need to read completely the chapter, and insert


func greet(person: String) -> String {
    let greeting = "Hello, " + person + "!"
    return greeting
}

By the way, that is a good learning exercise to learn to analyze error messages and try to figure out why greet(person:) is not recognized.


Read the book to fully understand the signature concept.

A function is not defined by its name ; it is defined by the complete signature.

Hence

func greetAgain(person: String) -> String { }

is different from

func greetAgain(person: String, again: Bool) -> String { }


It is even different from (see the _ )

func greetAgain(_ person: String) -> String {
    return "Hello you, " + person + "!"
}


let name2: String = greetAgain("Tim")

will give

"Hello you, Tim!"


Changing the return type also defines another func; consider the following example

func greetAgain(person: String) -> String {
    return "Hello again, " + person + "!"
}

func greetAgain(person: String) -> Int {
    return 10
}


These are 2 different func.

When you call, need to tell what type you expect:

let x: Int = greetAgain(person: "Tim")
let name: String = greetAgain(person: "Tim")


if you just try

let z = greetAgain(person: "Tim")

you get an error

Ambiguous use of 'greetAgain(person:)'

Because compiler (or playground interpreter) cannot guess which to use


Also, how can the this block of code (or is this called a code snippet? What's the difference, by the way?) be fixed, if the arguments/input value, 'alreadyGreeted' is changed to 'false' in line 14?


What is your point ? Just try

print(greet(person: "Tim", alreadyGreeted: false))

and see the result.