Skip to main content

Swift - Functions


  1. A chunk of code that performs an action or specific task.
  2. Can have in-out parameters.
  3. Can have no-parameters to complex parameters of different types.
  4. When a function say it will return a value, it must return a value by the end of function execution. But the function call can ignore return value.
  5. Functions can return multiple values using Tuple.
  6. Function parameters are constant by default., if you try to change the value of a parameter it will give a compiler error.
  7. If you ever want to modify a parameter value, mark them as in&out parameter type.
    1. InOut parameters must be variables., because constants can not be modified. 
    2. Variadic parameters can not marked as InOut.
    3. Place & before variable, while calling the function. 
    4. Write inout before parameter type while defining the function.
    5. InOut parameters is a way to have an impact of the function out side of its scope.
    6. func swapTwoInts(_ a: inout Int, _ b: inout Int)
    7. var someInt = 3
    8. var anotherInt = 107
    9. swapTwoInts(&someInt, &anotherInt)

  8. Functions can have argument label and parameter name. By default argument label can become parameter name.
    1.  func someFunction(firstParameterName: Int, secondParameterName: Int)
    2. func someFunction(argumentLabel parameterName: Int). With argument label and parameter name.
    3. func greet(person: String, from hometown: String) -> String. Use argument label for few parameters to make it more readable.
    4. func someFunction(_ firstParameterName: Int, secondParameterName: Int). Omit argument label by using _.
    5. func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12). With default parameter values. These parameters can be omitted while calling the function. So for readability place them at the end. Always define your function, with no parameters at the beginning and WITH parameters at the end.
    6. func arithmeticMean(_ numbers: Double...) -> Double Functions with VARIADIC parameters. Can accept zero or more values of defined type and the function implementation can read this parameter as a constant array of the type. Function can have at most one variadic parameter.
  9. Function Type: 
    1. Function Type as Return Parameter: 
      1. Function type can also be a return type of another function. Usually used to determine which function to use based on a condition. For example, whether to turn on or off a switch. 
      2. func chooseStepFunction(backward: Bool) -> (Int) -> Int. chooseStepFunction takes a Bool as input and returns a function of type (Int) -> Int.
    2. Function Type as Parameter Types:
      1. Function type can be a parameter of another function, this will make the caller to decide how to call this function. 
      2. func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int)
      3. Here mathFunction is of function type that takes two Ints and returns an Int.  When calling the printMathResult function, caller can choose to pass in any function that is of same type.
    3. func addTwoInts(_ a: Int, _ b: Int) -> Int {
    4. return a + b
    5. }
    6. func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
    7. return a * b
    8. }
    9.  Function takes two Int parameters as input and return Int.           
    10. (Int, Int) -> Int  is the function type.

    11. Can be used as any other types in Swift. Define a variable with function type and assign a function.

    12. var mathFunc: (Int, Int) -> Int = addTwoInts. This can be read as Defining a variable mathFunc of function type that takes two Int inputs and returns Int as output and set this to refer to addTwoInts function. Swift type checker will accept this.

    13. mathFunc(3, 2) ==> 5
  10. Nested Functions: Functions are usually global scope. But a function can encapsulate another function to keep the scope private and not visible to outside. Function can return one of its nested functions for global space to access and use it.