O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

For the More Curious: Void

The first function you wrote in this chapter was printGreeting(). It took no arguments and returned nothing. Or, did it?

It turns out that functions that do not explicitly return something actually do still have a return. They return something called Void. This return is inserted into the code for you by the compiler.

So, while you wrote printGreeting() like this:

func printGreeting() {
    print("Hello, playground.")
}

The compiler actually added something like this to your code:

func printGreeting() -> Void {
    print("Hello, playground.")
}

In other words, it added a return value of Void for you. Just what is Void? Go ahead and make printGreeting return Void, as shown above. Command-click on the word ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required