Functions In Swift

Introduction

 
Functions are self contained chunks of code that perform a specific task. Function in Swift has function parameter types and return types. It is easy to pass functions as parameters to other functions and to return functions from functions. 
 

Defining Function

 
Every function has a function name which describes the task that the function performs. To use the function, the function name should be called with its name and pass input values that match the types of the function parameter. A function argument must always be provided in the same order as the function parameter list.
  1. func sayHello(personName : String) - > String  
  2. {  
  3.     let greeting = "Hello, " + personName + "!"  
  4.     return greeting  
  5. }  

Function Parameter

 
Function parameter and return values are extremely flexible in swift. Anything can be defined from a single utility function with a single unnamed parameter to a complex function with expresive parameter names and different parameter options.
 

Multiple Input Parameters

 
Functions can have multiple input parameters written within the function paranthesis separated by commas. This function takes a start and end index for a half open range and the elements the range contains,
  1. func halfOpenRangeLength (start : Int, end : Int)    
  2. {    
  3.    return end - start    
  4. }    
  5.  println (halfOpenRangeLength(1 , 10))    
Output
 
9
 

Functions without Parameters

 
Functions are not required to define a return type. Versions of the function are called waveGoodbye, and it prints its own string value rather than returning it. It does not need to return a value, the function does not include the return arrow - > or return type. The first function prints a string, and returns the character count as an Int. The second function calls the first function but ignores its return value.The return value of a function is ignored when it is called,
  1. func printAndCount(StringToPrint: String) - > Int {  
  2.     println(StringToPrint)  
  3.     return countElements(stringToPrint)  
  4. }  
  5. func printWithoutCounting(StringToPrint: String) {  
  6.     printAndCount(StringToPrint)  
  7. }  
  8. printAndCount("hello , world")   

Function with Multiple Return values

 
The tuple type is used as return type for a function to return multiple values as part of one compound return value. The below count function counts the number of vowels, consonants and other characters in a string based on a standard set of vowels and consonants. The count function is used to count the characters in an arbitary string and retrieve the counted totals of three named Int values. The tuple members do not need to be named at the point that tuple is returned from the function because their names are already specified as part of function return type.
  1. func count(string: String) - > (vowels: Int, consonants: Int, others: Int) {  
  2.         var vowels = 0,  
  3.             consonants = 0,  
  4.             others = 0  
  5.         for character in string {  
  6.                 switch String(character).lowercaseString {  
  7.                     case "a""e""i""o""u":  
  8.                     ++vowels  
  9.                     case "b""c""d""f""g""h""j""k""l""m""n""p""q""r""s""t""v""w""x""y""z":  
  10.                     ++consonants  
  11.                     default:  
  12.                     ++others  
  13.                 }  
  14.                 urn(vowels, consonants, others)   

Function Parameter Names

 
It is useful to name each parameter when calling a function, to indicate the purpose of each argument that passed to the function. Define an external parameter name for each parameter in addition to local paramater name if users of function provide parameter names when calling the function. Write an external parameter name before the local parameter name it support separated by a space:
  1. func someFunction (externalParameterName localParameterName : Int)    
  2. {    
  3.   //function body    
  4. }     

Function types

 
Every function has a specific function type made up of parameter types and return type of the function. The example defines two simple mathematical functions called addTwoInts and multiplyTwoInts. These functions take two Int values and return an Int value which is the result of performing a mathematical operation. A function type has two parameters both of type Int and returns a value of type Int. The type of function is () - > ( ) or function has no parameters and returns void. 
  1. func addTwoInts(a: Int, b: Int) - > {  
  2.     return a + b  
  3. }  
  4. func multiplyTwoInts(a: Int, b: Int) - > {  
  5.     return a * b  
  6. }   

Nested Function

 
The defining of function inside the bodies of other functions is known as a nested function. Nested functions are hidden from from the outside by default, and used by enclosing functions. 
  1. func choose(backwards: Bool) - > (Int) - > Int {  
  2.     func step(input: Int) - > Int {  
  3.         return input + 1  
  4.     }  
  5. }   


Similar Articles