Site icon Meccanismo Complesso

Python Lessons – 3.2 Functions

Python Lessons - 3.2 Functions m
Python Lessons - 3.2 Functions

Functions

You have already seen and used the functions in the previous parts of the course. It is called a function call whenever we have used the following expression:

function_name()

For example, there are some features that we have already seen before

The word that precedes the parentheses is the function name, while the values or variables within the brackets are called arguments.

Function definition

So far you have seen and worked with already defined functions, but functions can be created using def.

You define a simple function that prints the first 5 numbers of the Fibonacci sequence

def fibonacci():
    i = 1
    n1 = 0
    n2 = 1
    print ( n1 + n2)
    while i < 10:
       n = n1 + n2
       print( n )
       n1 = n2
       n2 = n
       i += 1

fibonacci()

Executing the function you will obtain the sequence of the first 10 numbers of the Fibonacci sequence:

>>>
1
1
2
3
5
8
13
21
34
55

⇐ Go to Python Lesson 3.1 – Functions and modules – Code Reuse

Go to Python Lesson 3.3 – Functions arguments ⇒

Exit mobile version