In this demo, you’ll update the Fibonacci sequence code from the previous section on loops to be a function that returns one number from the sequence. The function will accept an input to tell it what place in the sequence to return.
Open Xcode on your Mac and create a new playground.
First, define a new function to contain all the code for the Fibonacci sequence:
func getFibonacciElement() {
}
This is a definition of a function with the name getFibonancciElement. It doesn’t take any input parameters or return an output. You’ll add all this later.
Inside the function, add the code from the previous demo:
var fibonacciSeries: [Int] = []
for index in 0...10 {
if index < 2 {
fibonacciSeries.append(index)
} else {
let element1 = fibonacciSeries[fibonacciSeries.count-1]
let element2 = fibonacciSeries[fibonacciSeries.count-2]
fibonacciSeries.append(element1 + element2)
}
}
print(fibonacciSeries)
This is the same code you wrote using a for loop to generate the first 11 digits of the sequence. There are two additional steps you need to add to finish the function:
- Provide it the index of the element you want to receive as an output.
- Make the code in the function return the element you asked for.
Start with passing an input. Change the function declaration to the following:
func getFibonancciElement(at inputIndex: Int) {
Notice the word at that is before the input parameter name. You learned that you can omit the name of a variable when you need to call the function by using underscore (_). You can also enter any word you want to change the name for the parameter when calling the function. Calling it looks like this:
getFibonacciElement(at: 5)
Inside the function, the variable name is still inputIndex, but when you call the function it looks like the variable is named at. This is something Swift provides to make the naming of functions clearer and closer to English.
Now that you have an input to the function, instead of generating the digits from zero to 10, change it to generate from zero to the provided index:
for index in 0...inputIndex
Before running the playground, you need to call the function so you can test the code. Without calling it, the playground is as good as empty. You defined a function but didn’t use it. Function definitions on their own don’t execute.
After the closing brackets of the function, call your function with 11 as an input:
getFibonacciElement(at: 11)
Run the playground, and you should see an array of 12 elements printed in the console.
The next part is to make the function return that last element. Change the function definition to return an Int:
func getFibonancciElement(at inputIndex: Int) -> Int {
Then, at the end of your function, replace the print statement with this line to return the element in the sequence:
return fibonacciSeries[inputIndex]
Now, change the calling of the function at the end of the Playground to be in a print and try different inputs:
print(getFibonacciElement(at: 11))
print(getFibonacciElement(at: 5))
print(getFibonacciElement(at: 1))
print(getFibonacciElement(at: 0))
Run the playground and look at the printed values in the bottom pane.
So far, your function is doing its job properly. There is only one small problem that could happen. Try getting the sequence element at index -1:
print(getFibonancciElement(at: -1))
Run the playground.
Accessing that element causes the playground to crash. It’s true that the input itself is wrong, because there is no such thing as the element in a negative index for the Fibonacci sequence. But crashing isn’t a good outcome from a function even if the input itself is corrupted.
What you can do in the beginning of the function is to validate the input and return zero if it’s a negative value. Add this at the very beginning of the function right after its declaration:
if inputIndex < 0 {
return 0
}
Now, run the playground. It doesn’t crash anymore, and the value zero is provided for the input -1.
Whether zero is the right output for a negative index or not is a different question, but it’s definitely better than a crash. Unexpected inputs will always happen, so it’s important your code can react without crashing. You can also add a print statement to mention in the console that an unexpected input was provided to the function. It would be a good idea to mention the function name in the message and maybe even the unexpected input value. That way its easy to know if any unexpected inputs are passed to your functions and which function received it.