Skip to main content

Functions

Updated Oct 28, 2019 ·

Overview

Functions let you group code to reuse it easily. You define them with a name, optional parameters, and a block of indented code.

Built-in Functions

Python has many built-in functions that perform common tasks.

For more information, please see Built-in Functions.

Examples:

  1. str() converts a value to a string::

      num = 10
    text = str(num)
    print(text) # Output: '10'
  2. int() converts a value to an integer:

    value = "25"
    number = int(value)
    print(number) # Output: 25
  3. float() converts a value to a float:

    value = "3.14"
    pi = float(value)
    print(pi) # Output: 3.14
  4. len() returns the length of a sequence:

    fruits = ["apple", "banana", "cherry"]
    print(len(fruits)) # Output: 3
  5. abs() returns the absolute value of a number:

    num = -7
    print(abs(num)) # Output: 7

Defining a Function

To create a custom function, we use def, which stands for "define", followed by function name and parentheses.

def my_function():
print("Hello, world!")

The functions names should also be short and descriptive. For example, a function that calculates the total of two numbers:

def calculate_total(a, b):
return a + b

To call the function:

my_function()
calculate_total(2, 3) # Requires two arguments

Output:

Hello, world!
5

The function body can also have multiple lines:

def average(numbers):
total = sum(numbers)
count = len(numbers)
return total / count

print(average([80, 90, 100])) # Output: 90.0

We can also store the result in a variable for later use.

average_sales = average([80, 90, 100])
print(average_sales) # Output: 90.0

Arguments/Parameters

Functions take arguments/parameters, which are values provided inside parentheses when calling a function.

In the example below, the square function takes a number as input, squares it, and prints the result.

def square(value):
new_value = value ** 2
print(new_value)

square(5) # Output: 25
square(10) # Output: 100

Argument Types

There are two main types:

  • Positional arguments - Provided in order, separated by commas. The function assigns them based on position.

    round(3.14159, 2)  # 3.14
  • Keyword arguments - Each argument is assigned using its name. This makes the function call clearer.

    round(number=3.14159, ndigits=2)  # 3.14

Default Arguments

Some functions have default arguments, which are used if no value is provided when calling the function.

A built-in example is round(). It accepts a decimal number and the second parameter that is optional. If the second parameter is not provided, it defaults to 0 decimal places.

num = 3.14159

print(round(num)) # Output: 3
print(round(num, 2)) # Output: 3.14

You can also define default arguments in custom functions.

In the example below, the function power_value uses the variable base and an optional variable exponent, which defaults to 1 if it is not provided.

def power_value(base, exponent=1):
return base ** exponent

Calling the function with two arguments overrides the default.

power_value(3, 2)   # Output: 9 

Calling it with one argument uses the default value.

power_value(3)   # Output: 3

Modifying the average Function

Using the average function from the Defining a Function section, we'll update it to include a keyword argument with a default value.

def average(values, rounded=False):  
avg = sum(values) / len(values)
return round(avg, 2) if rounded else avg

sales = [100, 200, 300]

print(average(sales)) # 200.0
print(average(sales, True)) # 200.00

Note:

  • Using positional arguments

    • average(sales, False)returns200.0`.
    • Omitting rounded gives the same result.
  • Using keyword arguments

    • average(values=sales, rounded=True) returns 200.00.

Return Values

Instead of printing a result, a function can return a value using return. This lets you assign the result to a variable.

In the example below, the square function returns the square of a number:

def square(value):
return value ** 2

num = square(6)
print(num) # Output: 36

Multiple Function Parameters

You can pass more than one argument to a function by defining multiple parameters in the function header. The order of arguments matters since they match the order of parameters.

For example:

def raise_to_power(base, exponent):
"""Returns the base raised to the power of exponent"""
return base ** exponent

print(raise_to_power(2, 3)) # Output: 8

Here, base is 2 and exponent is 3, so the function computes 2 to the power of 3.

Multiple Return Values

A function can return multiple values by creating a tuple. Tuples are immutable containers defined with parentheses.

For more information, please see Tuples.

Example:

def powers(a, b):
"""Returns a**b and b**a as a tuple"""
return (a ** b, b ** a)

result = powers(2, 3)
print(result) # Output: (8, 9)

You can then "unpack" the tuple and assign each value to separate variables in one line.

x, y = powers(2, 3)
print(x) # 8
print(y) # 9

The individual elements in a tuple can also be accessed using zero-based indexing, just like a list.

result = powers(2, 3)
print(result[1]) # 9

Indexing tuples is useful when you only need one specific value from a function’s multiple outputs.

Docstrings

Docstrings describe what a function does. They are written inside triple quotes (""") immediately after the function header.

For more information, please see Docstrings.

def square(value):
"""Returns the square of a value."""
return value ** 2

Recursive Functions

Recursive functions call themselves with modified parameters and must have a base case to stop the recursion.

  • The base case ends the recursion
  • The recursive case calls the function again with changed parameters

For example, a function to calculate factorial using recursion:

def recursive_function(n):
if n <= 1: # Base case
return 1
else:
return n * recursive_function(n - 1) # Recursive case

Output:

120

This shows how recursion repeats a process until the base case is reached.

Examples

  1. Rounding a Calculation

    We can round results before returning them.

    def average(numbers):
    return round(sum(numbers) / len(numbers), 2)

    Functions return a result using return.

  2. Arguments with Default Values

    This function replaces spaces with underscores and converts text to lowercase by default.

    def clean_text(text, lower=True):
    if lower == False:
    clean_text = text.replace(" ", "_")
    return clean_text
    else:
    clean_text = text.replace(" ", "_")
    clean_text = clean_text.lower()
    return clean_text
    if remove != None:
    clean_text = text.replace(remove, "")
    clean_text = clean_text.lower()
    return clean_text
    else:
    clean_text = text.lower()
    return clean_text

    To call the function:

    clean_text("ThIs iS a DirTy sEntENCe")

    Output:

    'this_is_a_dirty_sentence'  
  3. Data Structure Converter

    This function converts a collection into a list, set, or tuple based on the specified data_type. If no data_type is provided, it defaults to "list".

    def convert_data_structure(data, data_type="list"):
    if data_type == "tuple":
    data = tuple(data)
    elif data_type == "set":
    data = set(data)
    else:
    data = list(data)
    return data

    To call the function and store in a variable:

    texts = convert_data_structure({"a", 1, "b", 2, "c", 3})
    print(texts)
    print(type(texts))

    Since no data_type is provided, the function defaults to a list.

    [1, 'a', 3, 2, 'b', 'c']
    <class 'list'>

    If data_type="set", the function returns a set:

    texts = convert_data_structure({"a", 1, "b", 2, "c", 3}, "set")
    print(texts)
    print(type(texts))

    Output:

    {1, 'a', 3, 2, 'b', 'c'}
    <class 'set'>
  4. Check Disk and CPU Usage

    Get the script here: Sample python scripts.

    This Python script checks disk and CPU usage using the shutil and psutil modules:

    • shutil: disk_usage() to return disk statistics (total, used, and free space).

    • psutil: cpu_percent() function retrieves the current CPU usage percentage.

    Make sure to install psutil first:

    pip3 install psutil 

    To run the script:

    ./check-disk-cpu-usage.py

    If you encounter a "permission denied" error, it indicates the script lacks execute permissions. Fix this by updating the permissions:

    sudo chmod +x check-disk-cpu-usage.py

    Another way to run it is:

    python3 check-disk-cpu-usage.py