The function is a self-containing block of code for performing the specific task. The function is a thing, that we use every day. It’s like a building block for our code.
The general form of the Swift function is:
Let’s see it closer using sample:
Notation
Meaning
func
Denotes that this is a function
doSomethingCool
Name of the function
(foo: FooType, withBar bar: BarType, andBuz buz: BuzType)
Parameters of the function
foo, bar, baz
Parameters variable names
withBar, andBuz
Parameters names
FooType, BarType, BazType
Parameters types
ResultType
The type of the returned result. Can be empty (Void)
{}
Contains function body
Simplest function
The function can be simple. Let’s check the simplest one.
It starts with func keyword and named printMessage, does not have arguments nor return value. It just performs code contained inside curly braces {...}
Parameters syntax
The function can accept parameters. For example, the following function accepts singleString parameter with the name name.
The function can accept multiple parameters.
If you want to omit parameter names replace them with _.
That’s how previous function call will look like with second parameter name omitted:
Named parameters
If you want to make all parameters named, just type desired parameter name before the variable name. Just like in Objective-C.
That’s how previous function will look like with named first parameter:
Parameters with default values
The function can have default parameter values. If you skip parameter in a function call, it will take a default value. Otherwise, it will take passed value.
Parameter name from the next code sample has default value "Neo".
Variadic parameters
Variadic parameters are handy when parameters count is unknown. Variadic parameter is transformed to Array<ParamType>.
To mark parameter as variadic use name: <Type>... syntax. See the following sample:
In-out parameters
As mentioned above, variable parameters can only be changed by the function itself. If you need the function to change some parameters from outside the function scope, you should use inout keyword. Keep in mind that inout parameters cannot have default values.
Generic parameters
We can rewrite swapAgents(_:_:) function in a more general way. Using the following notation we tell that function accepts two parameters of any type, but the type of the parameter a is always the same as the parameter b type
Return values syntax
Functions can return values. It is not required (all the functions above did not return any values). Basically, functions are small programs, which can return results of their work.
As you can see, isThereASpoon(_:) function returns the single value of Bool type. In Swift, return value type is separated from function name with -> symbol.
Multiple return values
Sometimes we need to receive multiple return values from a function. It is possible. Swift allows us to deal with it in an elegant way. The following code sample shows how to use and access multiple return values:
Optional tuple
If there is probability, that returning tuple will have no value, we should use an Optional tuple. The function above, rewritten with optional tuple, will look like this:
Function type
You can pass any function as an argument or use it as a return value of any other function. In Swift, functions are first-class citizens. Function has a type. Function type general form is (argument types) -> return type. In the example below, both functions, plus(_: _:) and multiply(_: _:) has the same type: (Int, Int) -> Int. calculate(_: _: _:) function uses proper function depending on the operation passed as argument.
Function as return value
Function can also be used as return type of other function. mathFunction(_:) returns (Int, Int) -> Int function, which is used by calculate(_: _: _:) function from the previous example;
The type of the function can be named using typealias keyword. Let’s rewrite our math functions using typealas.
Nested functions
Nested functions are functions, used inside the other functions.
Conclusion
Functions in Swift are powerful and flexible. Different syntactical possibilities can be used to improve code informativity and reduce clatter.