Functions are a way to run your code on demand. You briefly explored functions in an earlier lesson. When associated with objects, functions are also known as methods. You used a function to print out a message. Functions do far more than that.
Functions also take in values and may return a value. For instance, an addition function may receive two integers. It will combine those integers and return the result.
When defining a function, you first define the inputs. You can pass whatever you like into the function. You simply define the variables in the function header:
This function requires two strings. When defining the parameters, you must always define the type. In this case, the function takes two strings but doesn’t return a value. You provide a return type before the opening curly brace.
You can use multiple return keywords in a single function. It is often the last statement in a function, but you can use it to “leave early”. If you do return out of a function early and that function returns a value, you must return a value.
See forum comments
This content was released on Apr 25 2025. The official support period is 6-months
from this date.
Explore how to create custom functions in SwiftUI
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Demo: Looping in SwiftUI
Next: Demo: Adding SwiftUI Functions
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.