Introduction
In this article I want to introduce some simple concepts about Swift Functions. Essentially, I want to explain what they are, and how a function is structured. I prefer to split the Function argument in different articles, rather than put all the concept in a very long, messy article. You should read the article, get the concepts, take the QUIZ and move on to the next article. These concepts are really important and you have to master it before moving on.
I’ve created a QUIZ, linked at the end of the article, to test your knowledge.
What is a Function?
A function is a self-contained chunk of code that perform a specific task. To complete that task a function may need some input or can return some output. Input and Output are not required, it’s fine to write a function that takes no input and return no output. Whether you should use it or not simply depends from what the function does.
Function’s Definition
In Swift, a function is composed by the following parts, put in order:
func
Keyword- Used to tell Swift that you’re declaring a function
functionName
- a descriptive name that describes what the function does
()
Input parameter list- The list of the Input parameters passed to the function wrapped in open and close parentheses (). Input paramenters are optional and the list can be empty, but even if no parameter are passed, you have to include an open and close parentheses.
->
Return arrow- Precedes the Output return Type, if present.
Output return type
- The Output that the function returns. It’s optional, so we can omit the
->
and the return type if a function doesn’t return an output.
- The Output that the function returns. It’s optional, so we can omit the
{}
Function’s body- The open and close curly brackets, wrap the function body. Within the function’s body you can write the code that is executed when the function is called.
Function without parameters
Let’s write a simple function that prints to console “Hello, World!”. In your Playground put the following code:
1
2
3
func helloWorld()-> Void {
print("Hello, World!")
}
In the helloWorld function we can see all the parts that were previously described.
- The func keywork
- helloWorld - the function’s name
- Input parameter list -
()
It’s empty because the function doesn’t take input ->
Return arrow- Void - Return type. Void is an empty tuple in Swift and is used to state that a function or a method don’t return a value.
- The function’s body wrapped in
{}
that prints the phrase “Hello, World!”
Since the function return no output, we can rewrite it in a simpler form, omitting the return arrow and the return type Void. Remember, every function return a value, a Void (that can be omitted) or another Type.
1
2
3
func helloWorld() {
print("Hello, World!")
}
The above function does the exact same thing than the previous one, just in a simpler form.
How to call a Function?
But now, how can we call this function? It’s simple: we call a function by using it’s name and passing all the input parameters it needs. In the case of the helloWorld function there are no input parameters so we simply call it with its name and an empty open and close brackets, indicating that there are no parameters to pass.
1
2
3
4
5
// Call the function to execute it
helloWorld()
// Now you'll see into the console "Hello, World!"
// >>> Hello, World!
Functions with parameters
A function can have multiple parameters, within the function parentheses, separated by commas. Let’s define a function hello(planet:) that take as input the name of the planet to greet and return a String with the customized greeting.
1
2
3
4
5
6
func hello(planet: String) -> String {
return "Hello, \(planet)!"
}
print(hello(planet: "Saturn"))
// Hello, Saturn!
This function take one parameter and return a String. Now, let’s define another function that takes two parameters, a planet name and the galaxy where it come from and return the greeting.
1
2
3
4
5
6
func hello(planet: String, galaxy: String) -> String {
return "Hello \(planet) from the \(galaxy)!"
}
print(hello(planet: "Jupiter", galaxy: "Milky way"))
// Hello Jupiter from the Milky way!
Notice that both the function are named “hello”, but the Swift compiler won’t bother because it can distinguish them by the input parameters. The first function take one parameter named planet of type String, while the second accepts two parameter, planet and galaxy, both of type String. A function can be distinguished by its name and its parameter list. This concept is called Overloading and the previous two functions are called overloaded functions.
And now, you can do the quiz to test your knowledge!!!
{Silvio Marotta}