Contents

Python Interactive Guide - Step 3 Functions (1) - Function Basics (1)

Series - Python Interactive Guide
Info
  • This course, Python Interactive Guide, is designed to help you learn the basics of Python programming through hands-on, interactive examples.
  • The “Style Guide” sections introduce clean coding practices, mainly based on PEP8.
  • You can run and experiment with every code example.
    Feel free to try things out - reloading the page will reset everything.

Starting from this section, we’ll learn about “Step 3 Functions”, which allows us to organize code into reusable components.

As programs grow larger, you’ll find yourself writing the same processes multiple times.
This is where functions come in handy.
By using functions, you can group a series of operations, name them, and call them whenever needed.

Functions are defined as follows:

Function Definition
def function_name(): function process
  • Functions are defined using the def keyword

  • The function name is followed by parentheses () and ends with a colon :

  • The function body is written in an indented block

  • The function naming rules are the same as those for Variable Naming Rules:

    1. Use letters (a-z, A-Z), numbers, and underscores (_)
    2. Do not start with a number
    3. Do not use reserved words (if, for, while, etc.)

Defined functions can be called and executed as follows:

Function Call
function_name()
  • Defined functions are called by writing the function name followed by parentheses ()
Style Guide: Function Definitions
  • Top-level function definitions should have 2 blank lines before and after them
    • For non-top-level functions (like nested functions), use 1 blank line before and after
    • You can add blank lines to make logical groupings more clear
  • Use function names that clearly indicate their functionality
  • In Python, function names typically use lowercase snake_case (words separated by underscores)
📚Exercise

Let’s create a program that achieves the same output by creating a function for the separator ("=" * 30) output shown below:

Example Solution

Next, let’s change the separator from equal (=) to hyphen (-).

Example Solution

Using functions offers the following benefits:

  1. Code Reuse: You don’t need to write the same process multiple times
  2. Improved Maintainability: When you need to change a process, you only need to modify the function definition
  3. Improved Readability: Naming a process makes the intention of the code clearer

Variables defined within a function are called local variables.
Conversely, variables defined outside are called global variables.

Local variables do not affect global variables:

The “range of accessibility” that determines where a variable can be accessed from is called scope.
We’ll discuss scope in more detail later.

Functions can return processing results as return values.
Return values are specified using the return statement.

Function Definition (with Return Value)
def function_name(): function process return return_value
Function Call (with Return Value)
variable_name = function_name() # Assign the return value to a variable

When a return statement is executed, the function ends immediately.
Any code after the return statement won’t be executed.

Multiple return statements can be used, allowing different return values based on conditions:

Since return statements end the function’s execution, the above code can be rewritten as follows:

Functions without a return statement, or with a return statement that doesn’t specify a value, return None:

You can return multiple values simultaneously by separating them with commas (,) in a return statement:

Function Definition (Multiple Return Values)
def function_name(): function process return return_value1, return_value2, ...
  • This actually returns a tuple
    (As we saw in Creating Tuples, tuple parentheses can be omitted)
  • You can use unpacking to assign the return values to separate variables
Function Call (Multiple Return Values)
variable1, variable2, ... = function_name()

In the program above, the width and height of the rectangle are fixed within the function.
To perform calculations for various rectangle patterns, we need to use parameters, which will be introduced in the next section.

📚Exercise

Create a function that returns both the minimum and maximum values of a list defined within the function,
then print both values on the screen.

Example Solution

Related Content