Links

In-Out Parameters | Swift 3

inout keyword is a powerful alternative to tuples for Swift and one thing to make a mistake.

PFB- Some notes worth considering
  1. When the function is called, the value of the argument is copied. (also known as copy-in copy-out or call by value result)
  2. In the body of the function, the copy is modified.
  3. When the function returns, the copy’s value is assigned to the original argument.
  4. In-out parameters cannot have default values.
  5. Variadic parameters cannot be marked as inout.
  6. We must use the ampersand "&" when passing arguments.

Sample:
/// inout

func check ( string: inout String) {
    string = "Innova"
}

var string = "hello"

print(string)
check(string: &string)
print(string)


Variadic Functions | Go by Example - Swift 3.0

Variadic functions can be called with any number of trailing arguments. Same like print(...) for Swift. Can be used with define type e.g. "String, Int.." or Any protocol.

/// variadic
func variadic (any: Any...) {
    
    if any.isEmpty {
        return
    }

    for val in any {
        print(val)
    }

}

variadic(any: "hello", "world", 2017, 2.0)


SieveSort Algorithm | Swift 3.0

SieveSort, based on the technique of divide-and-conquer, that takes time O(n2) in the worst-case. Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.
/// sieve algo
func sieve (numbers: [Int]) -> [Int] {

    if numbers.isEmpty {
        return []
    }
    
    let p = numbers[0]
    
    return [p] + sieve(numbers: numbers[1.. 0
    })

}

sieve(numbers: [2,3,4,5,6,7,8,9])