Learning Python: Part 3: Functions

Learning Python

Python is a general-purpose interpreted programming language. It emphasizes readability through the use of whitespace and supports multiple programming paradigms such as functional and object-oriented models.


In programming terminology, a function is a collection of statements. Often, it can useful to create a block of code that does a small or repeating task and then “call” this code to do something when needed.

When working with functions, data can be sent into it and data can be returned from it.

Defining Functions

The def keyword is used to define a function. The name of the function follows and then any parameters (data sent into the function) are next within open and close parentheses. The definition line of a function ends in a colon.

Statements that are part of a function in Python are placed at least one space away from the left margin underneath the function definition. Usually, they are tabbed in away from the left margin.

Calling Functions

Function are called through using their name and an open and close parentheses. To send data into the function, it is included within the parentheses with a comma between entries.

The print() function is commonly used, for example, to show data on the console. It is called through using its name and then sending data to it to display.

Returning Data

It is possible to get results and other data from a function through using the return keyword.

Through using the return keyword, a value can be “returned” from a function and saved.

Play with the example on Repl.it!