Functions In Python

This page will go over functions in Python.

Reference: Starting Out With Python – Second Edition By Tony Gaddis


What Is A Function?

A function is a group of statements designed to complete a task. Functions may take inputs and return outputs.


Defining A Function In Python

In Python, a function follows the following format.




A Function Call

A function call in Python involves typing in the function name along with its brackets. The call will execute the group of statements inside the specified function.


Multiple Functions

You can define multiple functions and make call functions within functions. In the example below, I define a say_goodbye() function and a hello_goodbye() function. In the hello_goodbye() function I call the say_hello() and the say_goodbye() functions.



Functions With Inputs / Arguments

In the examples above, the functions contained no inputs / arguments in the round brackets. No input was needed to run the functions.

The general form for functions with inputs / arguments is listed below.


Examples Of Functions With Inputs / Arguments


In the first example, I multiply two numbers together based on the inputted values of a and b.


The second example involves taking the square of a number.


In the final example, two numbers a and b are added together. The output is in the return line. The function call is saved to a variable called total and is then printed.



Leave a Reply