Functions can be declared without parameters or a return value. The only required information is a name (
hello in this case).
Call a function with no parameters by writing its name followed by an empty pair of parenthesis.
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:
has a function type of:
Function types can thus be used as parameters types or as return types for nesting functions.
Functions With Closures
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
Now that our function has been defined, let's call it and pass in some code:
By using a trailing closure with our function call, we can pass in code (in this case,
The log should print:
A more specific use case of this could include the execution of code between two classes:
The log should print:
Functions with Parameters
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.
\(number1) syntax is basic String Interpolation and is used to insert the integer into the String.
Functions with parameters are called by specifying the function by name and supplying an input value of the type used in the function declaration.
Any value of type Int could have been used.
When a function uses multiple parameters the name of the first parameter is not required for the first but is on subsequent parameters.
Use external parameter names to make function calls more readable.
Setting the default value in the function declaration allows you to call the function without giving any input values.
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.
This allows reference semantics to be applied to types which would normally have value semantics.
Instance methods are defined with a
func declaration inside the definition of the type, or in an extension.
increment() instance method is called on an instance of the
Type methods are defined with the
static func keywords. (For classes,
class func defines a type method that can be overridden by subclasses.)
Operators are Functions
Operators such as
?? are a kind of function named using symbols rather than letters. They are invoked differently from functions:
x + y
Passing and returning functions
The following function is returning another function as its result which can be later assigned to a variable and called:
Functions can return values by specifying the type after the list of parameters.
Functions can also return multiple values using tuples.
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the member elements of a collection, list, or sequence.
Subscripts can take any number of input parameters, and these input parameters can be of any type. Subscripts can also return any type. Subscripts can use variable parameters and variadic parameters, but cannot use in-out parameters or provide default parameter values.
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:
When you want to throw an error, use the
If you want to call a function that can throw an error, you need to use the
try keyword in a
For more on Swift errors: Errors
Trailing Closure Syntax
When the last parameter of a function is a closure
the function can be invoked using the Trailing Closure Syntax
Sometimes, it's not possible to list the number of parameters a function could need. Consider a
This works fine for finding the sum of two numbers, but for finding the sum of three we'd have to write another function:
and one with four parameters would need another one, and so on. Swift makes it possible to define a function with a variable number of parameters using a sequence of three periods:
.... For example,
Notice how the
numbers parameter, which is variadic, is coalesced into a single
Array of type
[Int]. This is true in general, variadic parameters of type
T... are accessible as a
This function can now be called like so:
A variadic parameter in Swift doesn't have to come at the end of the parameter list, but there can only be one in each function signature.
Sometimes, it's convenient to put a minimum size on the number of parameters. For example, it doesn't really make sense to take the
sum of no values. An easy way to enforce this is by putting some non-variadic required parameters and then adding the variadic parameter after. To make sure that
sum can only be called with at least two parameters, we can write