HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Functions

Functions

Definition with func, parameter labels, return, and default values.

Simple enough for a 5-year-old to understand

Define a function

Defined with func. -> specifies the return type. The _ in front of the parameter removes the label.

Example code
1
func square(_ x: Int) -> Int {
Takes an Int, returns an Int.
FunctionTakes an Int, returns an Int.
2
return x * x
Returns the result.
DefinitionReturns the result.
3
}
4
print(square(5))
Writes 25.
Output / InputWrites 25.
FunctionDefinitionOutput / Input
💡 Hint: _ removes the parameter label: you can write square(5).

About this topic

A function is a named block of code that performs a task. When you need to do the same task in multiple places, instead of copying the code, you turn it into a function; this way, if a fix is needed, you only change it in one place.

The most distinctive feature of Swift functions is argument labels. You are expected to write parameter names at the call site, which makes the call read like a sentence. If you don't want the label, you can remove it with an underscore, or give it a different external label.

Parameters are constant inside the function; you cannot change their values. This prevents the caller's data from changing unexpectedly. If you really need to modify the external value, you must use inout and explicitly mark it at the call site.

What you will learn

  • Defining and calling a function
  • Parameter and return types
  • The use of argument labels
  • Parameters with default values
  • Why parameters are constant

Who is this for?

  • Those looking to avoid code repetition
  • Those new to Swift's label syntax
  • Those getting errors when calling functions

When to use?

If a piece of code is written a second time, it should be extracted into a function; a third repetition should not be awaited.

A function should do a single task; if you have trouble naming it, it is probably doing more than one task.

Common mistakes

Wrong

func greet(name: String) {
  print("Merhaba \(name)")
}
greet("Ayşe")

Swift expects the argument label in the call; this code gives a "missing argument label 'name:' in call" error. Label requirement ensures that the person reading the call sees what the value means. You either need to write the label or remove it in the definition.

Correct

func greet(name: String) {
  print("Merhaba \(name)")
}
greet(name: "Ayşe")

Wrong

func double(value: Int) -> Int {
  value = value * 2
  return value
}

Parameters are constant inside the function; a "cannot assign to value: 'value' is a 'let' constant" error is received. Swift does this intentionally, because modifying the parameter gives the impression of an unexpected side effect for the caller. Creating a local copy is the correct way.

Correct

func double(value: Int) -> Int {
  let result = value * 2
  return result
}

Wrong

func sum(a: Int, b: Int) -> Int {
  print(a + b)
}

Even though the function declares it will return an Int, return was not written; a "missing return in global function expected to return 'Int'" error is received. Printing to the screen and returning a value are different things: you cannot use a printed value in another calculation.

Correct

func sum(a: Int, b: Int) -> Int {
  return a + b
}

Frequently asked questions

How do I remove the argument label?

You put an underscore and a space in front of the parameter name. Thus, you only write the value in the call. This is preferred in mathematical functions or situations where the label adds nothing.

How do I define a parameter with a default value?

You write an equals sign and a value after the parameter type. You can omit that parameter in the call. In Swift, parameters with default values do not have to be at the end, because labels already determine which value goes to which parameter.

Can I return multiple values?

Yes, by using tuples. You write the return type as multiple types inside parentheses, and you can also name the elements. If you are returning more than two values, defining a struct is usually more readable.

Summary

Functions are defined with func, and the return type is written with an arrow.

Argument labels are mandatory in the call.

The label can be removed with an underscore.

Parameters are constant inside the function.

If a return type is declared, return is mandatory.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code