Tutorial by Examples

Functions can be declared without parameters or a return value. The only required information is a name (hello in this case). func hello() { print("Hello World") } Call a function with no parameters by writing its name followed by an empty pair of parenthesis. hello() //output...
Functions can take parameters so that their functionality can be modified. Parameters are given as a comma separated list with their types and names defined. func magicNumber(number1: Int) { print("\(number1) Is the magic number") } Note: The \(number1) syntax is basic String I...
Functions can return values by specifying the type after the list of parameters. func findHypotenuse(a: Double, b: Double) -> Double { return sqrt((a * a) + (b * b)) } let c = findHypotenuse(3, b: 5) //c = 5.830951894845301 Functions can also return multiple values using tuples. f...
If you want a function to be able to throw errors, you need to add the throws keyword after the parentheses that hold the arguments: func errorThrower()throws -> String {} When you want to throw an error, use the throw keyword: func errorThrower()throws -> String { if true { retur...
Instance methods are functions that belong to instances of a type in Swift (a class, struct, enumeration, or protocol). Type methods are called on a type itself. Instance Methods Instance methods are defined with a func declaration inside the definition of the type, or in an extension. class Coun...
Functions can modify the parameters passed to them if they are marked with the inout keyword. When passing an inout parameter to a function, the caller must add a & to the variable being passed. func updateFruit(fruit: inout Int) { fruit -= 1 } var apples = 30 // Prints "There's 3...
When the last parameter of a function is a closure func loadData(id: String, completion:(result: String) -> ()) { // ... completion(result:"This is the result data") } the function can be invoked using the Trailing Closure Syntax loadData("123") { result in ...
Operators such as +, -, ?? are a kind of function named using symbols rather than letters. They are invoked differently from functions: Prefix: -x Infix: x + y Postfix: x++ You can read more about basic operators and advanced operators in The Swift Programming Language.
Sometimes, it's not possible to list the number of parameters a function could need. Consider a sum function: func sum(_ a: Int, _ b: Int) -> Int { return a + b } This works fine for finding the sum of two numbers, but for finding the sum of three we'd have to write another function: f...
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence. Example struct DaysOfWeek { var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri"...
Using functions that take in and execute closures can be extremely useful for sending a block of code to be executed elsewhere. We can start by allowing our function to take in an optional closure that will (in this case) return Void. func closedFunc(block: (()->Void)? = nil) { print("...
The following function is returning another function as its result which can be later assigned to a variable and called: func jediTrainer () -> ((String, Int) -> String) { func train(name: String, times: Int) -> (String) { return "\(name) has been trained in the Force \(times)...
Every function has its own function type, made up of the parameter types and the return type of the function itself. For example the following function: func sum(x: Int, y: Int) -> (result: Int) { return x + y } has a function type of: (Int, Int) -> (Int) Function types can thus be use...

Page 1 of 1