Skip to content

7.1 Distinguish between functions and modules

Function refers to the isolation of a part of the program’s functionality and make it reusable.

Module refers to the collection of functions in one file which can be used in various projects.

  Functions and modules  

7.1a Full definition of a Python function

A Python function is a block of reusable code designed to perform a specific task. Functions are defined using the def keyword, followed by the function name, parentheses (which can enclose parameters), and a colon. The indented block of code following the colon is the function body.

Syntax:

def function_name(parameters):
    # Function body
    # Perform some actions
    return result  # (optional)

 


 

7.1b Why use functions?

  • Code Reusability - Functions allow you to reuse code without rewriting it multiple times.
  • Modularity - Functions break down complex problems into smaller, manageable parts.
  • Readability - Functions improve the readability and organization of code.
  • Maintainability - Functions make code easier to maintain and update.

Example 7.1.1 - Python function

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

result = add(2, 3)  # result will be 5

 


 

7.1c Full definition of a Python module

A Python module is a file containing Python definitions and statements. Modules are used to organize code into manageable sections. They can define functions, classes, and variables, and they can also include runnable code. Modules are saved with a .py extension.

 


 

7.1d Why use modules?

  • Code Organization - Modules help to organize related code into a single file.
  • Namespace Management - Modules prevent naming conflicts by encapsulating code in a separate namespace.
  • Reusability - Modules can be imported and used in different programs, promoting code reuse.
  • Maintainability - Modules make it easier to maintain and update code, as changes in a module affect all scripts that import it.

Example 7.1.2 - Python module

Suppose we have a file named mymodule.py

# mymodule.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

You can import and use this module in another script. Please refer to section 7.5 - Import modules to find out more ways how to import modules.

import mymodule

result_add = mymodule.add(5, 3)  # result_add will be 8
result_subtract = mymodule.subtract(5, 3)  # result_subtract will be 2
 


 

7.1e Distinguishing between functions and modules

  • Scope and Purpose:

    • Functions:

      • A function is a single piece of code designed to perform a specific task.
      • Functions are defined within a script or module.
      • They can take parameters and return results.
      • Functions are called or invoked to execute.
    • Modules:

      • A module is a file containing Python code (which can include multiple functions, classes, and variables).
      • Modules are used to organize and encapsulate related code.
      • They are imported into other scripts to use the code they contain.
      • Modules can contain executable statements as well as definitions.
  • Use Cases:

    • Function Use Case - You need to repeatedly perform a specific task, such as calculating the sum of two numbers.
    • Module Use Case - You have a collection of related functions and classes, such as a set of mathematical operations, and you want to organize them in a single file for reuse.

 

Example 7.1.3 - To illustrate the difference between functions and modules.

# module_example.py (a module)
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

# another_script.py (a script using the module)
import module_example

result1 = module_example.add(10, 5)        # Using the add function from module_example
result2 = module_example.subtract(10, 5)   # Using the subtract function from module_example