CHAPTER 8

image

Functions

We have already shown the functions that are built into Python. Although there is a wealth of functions available to us, there will be times that you need to create your own. In some other programming languages, functions are known as subroutines.

There are usually two reasons for functions. The first is to organize the code into a logical way to handle certain tasks. The other is to be able to reuse code. The general rule of thumb is that if you have a block of code that gets called more than once, put it in a function.

Structure of a Function

The structure of a function is very simple but very important.

def {FunctionName}[(parameters)]: ...

Get The Python Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.