Intermediate · Functions · 113 of 264
Fibonacci number
The Fibonacci numbers start 0, 1 and each next one is the sum of the previous two: 0, 1, 1, 2, 3, 5, 8, ... Write fib(n) returning the n-th one (fib(0) is 0).
Input: a whole number n from 0 to 90. Output: fib(n)
Examples
Input
10
Output
55
Input
0
Output
0
Input
1
Output
1
2 more tests check other inputs when you press Check.
Stuck? The idea behind functions
A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.
A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.
def is_even(n):
return n % 2 == 0
print(is_even(4))
print(is_even(7))Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.