What a function is
A function is a named block of code that does one job. Instead of writing the same lines wherever you need them, you write them once inside a function and use the function's name when you want them to run. Functions also let you split a long program into small pieces that are each easy to read.
Here is the shape of a function definition in Swift:
func functionName() {
// function body
}
It has two parts:
- The header. The keyword
func, then the function's name, then a pair of parentheses. The parentheses are empty for now. Later lessons put inputs there. - The body. The statements the function runs, wrapped in curly braces
{}.
A complete example:
func sayHello() {
print("Hello World")
}
The body is indented by one level. Swift doesn't require indentation, but it makes it obvious which lines belong to the function.
This function has no -> Type after the parentheses, so it returns nothing. It only has an effect, printing a line.
Defining is not running
If you put only the definition above in a program, nothing is printed. A definition describes what the function does, and the body doesn't execute until something calls the function. A call is the function's name followed by parentheses:
func sayHello() {
print("Hello World")
}
sayHello() // Hello World
sayHello() // Hello World
Each call runs the whole body again, from the first statement to the last, so a function can be called as many times as you need.
The parentheses matter. Writing just sayHello refers to the function itself instead of calling it, and Swift reports a statement like that as an error: function is unused.
The exercise, step by step
The starter code gives you an empty mainFunction, the lesson's main program. You add a function above it and call it from inside.
- Define
sayHelloabovemainFunction. Writefunc sayHello() { }. At this point it exists but does nothing. - Fill in the body. A single
print("Hello everybody!")goes between the braces.printends its output with a new line. - Call it from
mainFunction. Without the callsayHello()the definition would never run, so nothing would be printed.
The solution in Swift
func sayHello() {
print("Hello everybody!")
}
func mainFunction() {
sayHello()
}
Notice that mainFunction is itself a function, and it calls another one. Functions calling functions is how larger programs are built.
The order of definitions at the top level of a file is flexible for functions, so sayHello could even be declared below mainFunction. Keeping helpers above the code that uses them reads more naturally, and the lesson does it that way.
Common mistakes
- Forgetting the braces. The body must be inside
{and}. A header with no body is a compile error. - Forgetting the call. If the function is defined and the output is empty, check that something calls it.
- Calling without parentheses.
sayHellois not a call. WritesayHello(). - Misspelling the name. Swift is case-sensitive, so
sayhello()andsayHello()are different names. - Wrong text. The tests compare the printed text exactly, including capital letters and the exclamation mark.
Key takeaways
- A function is defined with
func, a name, parentheses and a body in braces. - A definition only declares the function. A call such as
sayHello()runs it. - A function with no return type returns nothing, and every call runs the full body.
