Content Index
- What is a function in Swift and what is it for?
- Why functions avoid repeated code
- Key differences between functions in Swift and Kotlin
- How to declare a function in Swift step by step
- The func keyword and its basic syntax
- The function body and the braces {}
- Passing parameters in Swift functions
- How to execute a function in Swift
- Labels in Swift: what they are and why they are mandatory
- Common errors when passing parameters
- Default values for parameters
- Functions that return values in Swift
- Practical example: summing values and displaying the result
- Conclusion
When you start programming in Swift, besides knowing Xcode, one of the first concepts you must master is functions. Not only because they appear in every example, but because they are the foundation for writing clean, reusable, and easy-to-maintain code.
In my case, before getting into Swift, I had already worked with other languages, and that helped me quickly understand the general idea… but it also made me bump into some important differences that Swift has compared to other languages like Kotlin. It is precisely these differences that usually cause the most errors at the beginning.
What is a function in Swift and what is it for?
A function in Swift is a block of code that performs a specific task and that you can reuse as many times as you want. Instead of repeating the same instructions over and over again, you encapsulate 그 logic in a function and simply call it whenever you need it.
Why functions avoid repeated code
Without functions, any action that repeats would end up being copied and pasted in different parts of the code. This not only makes the code longer but also harder to maintain. If something changes, you would have to fix it in many different places.
Functions solve this problem by centralizing the logic in a single place.
Key differences between functions in Swift and Kotlin
While in Kotlin you define functions with the reserved word fun, in Swift, func is used. It is a small detail, but enough to make a mistake the first few times if you come from another language.
Functions are pieces of code that do something, a particular task; unlike Kotlin, where we define a function with the reserved word
fun, in Swift we create a function withfunc; that is, we add one more 'c' to our reserved word that refers to the definition of a function in Swift.
How to declare a function in Swift step by step
The func keyword and its basic syntax
The simplest way to declare a function in Swift is this:
func hola_mundo() { }As you can see, in the previous lines we defined our first function in Swift, where the body of the function is defined by the content enclosed by the braces {} which in this example is a simple print; to call or execute the function, we must perform the following operation:
hola_mundo() Although for our example, which is actually quite unexciting, it does nothing, you won't see any response in the console coming from this function because at the moment it is a function that has nothing defined in its body or structure.
- func indicates that you are defining a function.
- hola_mundo is the name of the function.
- The parentheses () indicate that it does not receive parameters.
- The braces {} contain the body of the function.
In one of my first examples, I left the function completely empty, and when I executed it, absolutely nothing happened. That helped me understand that defining a function does not automatically execute it.
The function body and the braces {}
Everything inside the braces is what will execute when the function is called. For example:
func hola_mundo() { print("Hello world") }Passing parameters in Swift functions
The previous format is the simplest thing we can do to execute a function; it can also receive what are known as parameters, which in essence is a definition of a variable indicating the data type:
func hola_mundo(name:String) { }How to execute a function in Swift
Once the function is defined, executing it is as simple as writing its name followed by parentheses:
hola_mundo()If you forget this step, the function simply never executes. This seems obvious, but when you are starting out it is easy to think that the code "should already do something" just because it is written.
And with a parameter:
hola_mundo("My first function with Swift");Labels in Swift: what they are and why they are mandatory
Unlike Kotlin, where this is optional, in Swift, labels are part of the language design. This makes the code more readable, but it is quite confusing at first.
But if we do this, it will give us an error as we can see in the following image:

Since we have to specify what would be the label before placing our value, that is, the name of the function parameter:
func hi(name:String) { print(name) }
hi(name:"My first function with Swift");This is an important difference that Swift has with Kotlin, since in the latter, specifying the label or parameter name of a function is completely optional.
In addition to this, let's add a console print to see our message:

Common errors when passing parameters
- Forgetting the parameter label.
- Using an incorrect data type.
- Confusing the parameter name with the value being passed.
All these errors are normal when you start, and they are almost always related to not respecting the exact syntax that Swift expects.
Default values for parameters
We can also set what would be default values in the function parameters; for this, we use the following syntax:
func hi(name:String = "Andres"){ print(name) }
hi()As you can see, now the parameter passing is optional:

And let's go a bit further and concatenate a full text in our print:

Also remember that we can interpolate the texts, as we explained in the previous Swift post:
func hi(name:String = "Andres") { print("Hello \(name), how are you?") }
hi()Functions that return values in Swift
Of course, like every function in Swift, we can indicate a return value; for that, we use the arrow function followed by the data type to return; and of course, the typical return with the returned value:

In addition to executing actions, a function can return a value. For this, the arrow -> followed by the type of data that will be returned is used.
func add(a: Int, b: Int = 5) -> Int { return a + b }Here the function returns an Int.
Practical example: summing values and displaying the result
func add(a:Int, b:Int = 5) -> Int { return a + b } let a = 4 let b = 4 suma(a:a,b:b)
Swift is extremely explicit: everything must be well-defined, from the types to the value the function returns.
And to show a slightly more complete example, we present the same previous function but with String interpolation:
func add(a:Int, b:Int = 5) -> Int { return a + b } let a = 4 let b = 4 print(" Sum of \(a) + \(b) = \(add(a:a, b:b))");
Best practices when working with functions in Swift
- Use clear and descriptive names
- A function's name should explain exactly what it does. This is even more important in Swift, where labels make functions read almost like sentences.
- Avoid functions that are too large
- If a function starts to grow too much, it is probably doing more than one thing. At that point, it is advisable to split it into smaller, more specific functions.
- This habit not only improves the code but also makes debugging much easier when something doesn't work as you expected.
Conclusion
Functions in Swift are a fundamental piece of the language. Although their syntax may seem strict at first —especially with labels and types— that same rigidity is what allows for clearer, safer, and more maintainable code.
If you come from another language, it is normal to make mistakes at the beginning. The important thing is to understand why Swift works this way and use those rules to your advantage.
Now, learn how to use conditionals in Swift.