Function, Module, Exception

HSS 611: Programming for HSS

Taegyoon Kim

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

Body and return

  • The body contains the code to be executed when the function is invoked
  • The function usually returns something
    • This is done with the return keyword
    • After return is invoked, the function is exited

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

  • What will this print?

Function

Variable scope

  • How about this?

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

Variable scope

  • Again, be careful with function scope
  • What would it return?

Function

Function with many return statements

  • return can only be used inside of a function
  • There can be multiple returns 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

Function

Function with many return statements

  • Another example

  • What will it return?

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

Example

  • math_operations module: math_operations.py

Module

Example

  • To use a function from the module, you need to refer to what we imported

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

  • Standard library example

Exception

Syntax errors and exceptions

  • When there is something wrong with syntax, Python will throw a syntax error

Exception

Syntax errors and exceptions

  • But even without a syntax error, there can be exceptions, which are errors during execution

Exception

Handling exceptions

  • If your code can encounter an exception, you can handle that using try-except block
  • After except, you specify the action to be taken if the exception occurs

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

  • Similarly

Exception

Multiple exceptions are possible

  • Also

Exception

Multiple exceptions are possible

  • Even

Exception

If you know the exact source of an error or the scope of potential errors in advance

  • You can also use if to prevent (rather than handle) them (see discussions here)

Exception

We may want to “raise” (or throw) reasonable exceptions

  • It’s useful to raise exceptions to signal that something went wrong and stop execution in a controlled way

Exception

We may want to “raise” (or throw) reasonable exceptions

  • Alternatively

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