Using a single-parameter function
The syntax that's used to define a function in Swift is very flexible. This flexibility makes it easy for us to define simple C-style functions, or more complex functions with local and external parameter names, which we will see later in this chapter. Let's look at some examples of how to define functions. The following example accepts one parameter and does not return any value to the code that called it:
func sayHello(name: String) -> Void {
let retString = "Hello " + name
print(retString)
}
In the preceding example, we defined a function named sayHello()
that accepted one parameter, named name
. Inside the function, we printed out a greeting to the person. Once the code within the function is executed, the function exits, and control is returned to the code that called it. Rather than printing out the greeting, we could return it to the code that called it by adding a return type, as follows:
func...