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.


  1. The best online casino sites for 2021
    Best online 메리트 카지노 주소 casino for real money: bet365 1xbet Casino · InterTops 온카지노 – Best Online Casino for Slots with Free Spins · Jackpot City – Best Online Casino for

  2. New on line casino platforms are popping out every month, and roulette followers are spoilt for selection. Now find a way 온라인 카지노 to|you presumably can} play European, American, and all different types of roulette regardless of your geographical location. Also, there are appreciable advantages to half in} the game online, rather at a brick-and-mortar on line casino.


Post a Comment

Popular posts from this blog

Swift - Structure and Classes

Structures and Classes: How are structures different ? No inheritance.  No type casting to check at runtime.  No reference counting. No multiple references.  No deinitializers.  Structures are value types. When you assign instance of a struct to another variable, the entire structure is COPIED and not REFERENCED. Classes instead are REFERENCE types, pointing to the same instance.  === operator is to know if two variables / constants refer to the same instance.  == means they are equal in some value. It is your responsibility to implement == and != in your class.

Swift - Closures

Closures: 1. These are self contained blocks of code that can be passed around in the code. Declare: (parameters) -> return Define: {    ( parameters ) -> return type in      // executable statements } reversedNames = names . sorted ( by : { ( s1 : String , s2 : String ) -> Bool in return s1 > s2 } ) 1. Inferring type from context     reversedNames = names . sorted ( by : { s1 , s2 in return s1 > s2 } ) 2. Implicit returns from Single-expression closures     reversedNames = names . sorted ( by : { s1 , s2 in s1 > s2 } ) 3. Short hand argument names. Can omit variable name, return and in key word as well.     reversedNames = names . sorted ( by : { $0 > $1 } ) 4. Operator methods. Simply use the operator. reversedNames = names . sorted ( by : >) 5.Trailing closures. If closure is the final argument of a function, it can be passed as a trailing closure. func someFunctionThatTakesAClosure ( closure : () -> V