Function, Module, Exception
HSS 611: Programming for HSS
Sep 11, 2025
Agenda
- Function
- Module
- Exception
- Reading and writing text file
Function
Bad programming
- Repeatedly copying, pasting, and modifying the same code lines can be
- Problematic: prone to errors while editing
- Inefficient: (really) long lines of code
Function
Good programming
- Write more succinct code by writing functions (reusable pieces of code)
- Makes code more maintainable and efficient
- If you anticipate repeating the same or very similar code, it may be worth writing a function
Function
Components
- Has a name
- Has arguments / parameters
- Has a docstring (optional but recommended)
- Has a body
- Returns something
Function
Simple example
Function
def
is the keyword used to define a function
Name of the function comes after def
- In this case,
is_even
is the name
Then, inside ( ), comes the argument(s) / parameter(s)
- In this case,
i
is the only argument / parameter
Function
Docstring
- Enclosed in
"""
,
- Provides information on how to use the function
- Docstrings can be accessed with
help()
Function
Variable scope
- In Python (and other programming languages), we assign values to variables
- Scope refers to the region where a variable can be accessed or modified
- Initially / by default, we are in the global scope
- When a function is entered, a new (local or function) scope is created
Function
Variable scope
Function
Variable scope
Function
Variable scope
- Notice, the x became 9 in the function’s scope; not globally
Function
Variable scope
- Here, because
x
is not one of the arguments, looks for an x
in the global environment
Function
Function with many return statements
return
can only be used inside of a function
- There can be multiple
return
s in a function
- Only one of them will be used each time function is invoked
- Once any
return
is hit, function’s scope is exited and nothing else in the function is run
Function
Function with many return statements
- Will hit one of the three returns depending on
number
Module
Modules are files (.py) that (mainly) contain function definitions
They allow us to organize, distribute code; to share and reuse others’ code too
Keep code coherent and self-contained
One can import
modules or some functions from modules
Module
Alternatively you can do the following
Module
You can rename function while importing
Module
There are many modules in the Standard Library and external libraries that one can and should use
Exception
Multiple exceptions are possible
- With the error type specified, the except block runs only when an error of the specified type occurs
Exception
Multiple exceptions are possible
Exception
Multiple exceptions are possible
Exception
Multiple exceptions are possible
Reading and writing text file
Write a regular text file
Reading and writing text file
Read it back in and check
Reading and writing text filessss
The more preferred syntax: with()
open(), read() / write(), close()
is a bit cumbersome
Reading and writing text file
readlines()
- Reads individual lines into a list
Reading and writing text file
read()
- Reads the entire text as a string