What is a Function?
Functions are self-contained chunks of code that perform a specific task. You can supply functions with additional parameters to customize functionality.
Declaring Functions
Calling Functions
To execute the code within a function, you have to call the function.
Functions with Parameters
Parameters allow you to inject value(s) into the function.
When declaring a parameter, you have to provide an explicit type for the value.
In the following example, the sayHello()
function can be modified to inject a name
value. This allows the sayHello()
function to be reused for different names.
To call the function, you will now have to supply the function with the name
parameter.
Multiple Parameters
Multiple different parameters can be declared, separated by commas. This allows you to inject multiple different values into the function.
In the following example, an additional hobby
String value is also injected into the function.
Likewise, when you call the function, you will have to also supply it with the hobby
parameter.
Argument Labels
An argument label is a variable name that you provide in the function declaration to be used when the function is called. Swift heavily emphasizes writing readable code that sounds as similar to English as possible.
You can see the argument label as the "public name" and the parameter name as the "internal name".
You can declare an argument label by supplying one preceding the parameter name.
In the following example,
the parameter name,
name
, has an argument label offor
, andthe parameter name,
hobby
, has an argument label ofwithHobby
.
Naming a variable for
or withHobby
might not make sense within in your code, but when used as part of the function, it helps to make the statement more readable.
When you call the function, the argument label is used instead of the parameter name.
Omitting Argument Labels
You can omit an argument label entirely by supplying an underscore (_
) as the argument label name.
When calling this function, you do not need to use an argument label for the name
parameter.
Function Types
Like variables, functions can also have types. This allows the function to evaluate and return a result.
To declare a function with a type, add a ->
followed by the type name after the function's parameters.
If there are no parameters, add the return type after the parenthesis.
In the following example, the calculateBMI
function takes in 2 values—a weight (in kilograms) and height (in meters), calculates the body mass index, and returns the value.
Implicit Return
If the function only contains a single line, the return
keyword can be omitted. This is called an implicit return as Swift infers that the only line is the returned value.
Functions & SwiftUI
Functions can be used within SwiftUI to help break down large chunks of conditional logic or break apart large views.
Creating a Function in SwiftUI
When creating a function within SwiftUI, it is best to declare it outside of the view's body
Computed Property, while still within the view structure.
Calling the Function within SwiftUI
By default, a function is Logical Code. It can only be used within places like a Button's action or with the onAppear modifier. Calling a function in SwiftUI is exactly the same as calling a function within Swift.
Using a Function as an Action
Functions can be supplied directly to controls like Buttons as actions.
In the following example, the Button to run the doSomething
function when tapped and a similar example with the onAppear modifier.
Breaking Large Views with Functions
Functions can be used to break up large view files into smaller functions.
By setting the function's type as some View
, the function can return View Builder code, allowing it to be used as part of composing your interface.
The following example shows how a function can be used to apply repeated modifiers on a text, reducing the amount of repeated code.