Using a single parameter function

The syntax 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. Let's look at some examples of how to define functions. The following example accepts one parameter and does not return any value back 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 name of the person. Once the code within the function is executed, the function exits and control is returned back 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 sayHello2(name: String) ->String {  
  let retString = "Hello " + name  
  return retString 
} 

The -> string defines that the return type associated with the function is a string. This means that the function must return an instance of the String type to the code that calls it. Inside the function, we build a string constant named retString with the greeting message and then return it using the return statement.

Calling a Swift function is a similar process to calling functions or methods in other languages, such as C or Java. The following example shows how to call the sayHello(name:) function that prints the greeting message to the screen:

sayHello(name:"Jon") 

Now, let's look at how to call the sayHello2(name:) function that returns a value back to the code that called it:

var message = sayHello2(name:"Jon")  
print(message) 

In the preceding example, we called the sayHello2(name:) function and inputted the value returned in the message variable. If a function defines a return type as the sayHello2(name:) function does, it must return a value of that type to the code that called it. Therefore, every possible conditional path within the function must end by returning a value of the specified type. This does not mean that the code that called the function is required to retrieve the returned value. As an example, both lines in the following snippet are valid:

sayHello2(name:"Jon") 
var message = sayHello2(name:"Jon") 

If you do not specify a variable for the return value to go into, the value is dropped. When the code is compiled you will receive a warning if a function returns a value and you do not put it into a variable or a constant. You can avoid this warning by using the underscore, as shown in the following example:

_ = sayHello2(name:"Jon") 

The underscore tells the compiler that you are aware of the return value but you do not want to use it. Using the @discardableResult attribute when declaring a function will also silence the warning. This attribute is used as follows:

@discardableResult func sayHello2(name: String) ->String { 
  let retString = "Hello " + name 
  return retString 
} 

Let's look at how we would define multiple parameters for our functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset