Flocode: Engineering Insights 🌊

Flocode: Engineering Insights 🌊

Share this post

Flocode: Engineering Insights 🌊
Flocode: Engineering Insights 🌊
#074 - Python Functions for Engineers: The Building Blocks of Code

#074 - Python Functions for Engineers: The Building Blocks of Code

Learn to to automate, organize, and optimize your code.

James O'Reilly's avatar
James O'Reilly
Jul 22, 2025
∙ Paid
2

Share this post

Flocode: Engineering Insights 🌊
Flocode: Engineering Insights 🌊
#074 - Python Functions for Engineers: The Building Blocks of Code
1
Share

Every structure involves the same fundamental calculations: load combinations, unit handling, material property lookups. Yet many engineers rebuild these calculations from scratch for each project, leaning on the curation of fragile Excel spreadsheets, whiteknuckling through inconsistent methods across teams, projects, clients and sub-contractors.

This article is a quick refresher on Python functions. It’s always worth revisiting the fundamentals.


Why Functions?

Functions help us break down complex engineering tasks into modular, manageable parts, making our code efficient, reusable, and scalable.

We can chain tasks, output from one function flows straight into the next, so our workflow can be entirely automated once we understand the logic.

They are the building blocks of programming, and can be as complex or as simple as needed.

A function typically includes:

  • Name: Identifies the function.

  • Parameters: Variables that accept inputs to the function.

  • Body: Contains the code to be executed. It could be as simple as 1 + 1 or as complex as two black holes colliding.

  • Return statement: Outputs the result of the function (if required).

With functions, we avoid rewriting code, streamline repetitive tasks, and make our codebase more organized and adaptable.


2. Understanding Function Syntax in Python

The basic syntax for defining a function in Python is:

def function_name(parameters):
    # Function body
    return result

Parameters and Arguments

  • Parameters: Variables specified in the function definition (placeholders for data the function will use).

  • Arguments: Actual values passed to the function when calling it.

For example, a function to calculate the stress in a material might look like this:

def calculate_stress(force, area):
    stress = force / area
    return stress

Return Values

The return statement outputs the result, making it available for further operations. This is especially useful in workflows that require multi-step calculations, where one function’s output feeds into another’s input.


3. Automating Tasks with Functions

Functions enable us to automate repetitive calculations and tasks. For engineers, this means quicker computations and fewer errors.

Consider a function to convert a list of forces from kilonewtons (kN) to newtons (N):

def convert_kn_to_n(forces):
    return [force * 1000 for force in forces]

Using this function, we can convert a list of forces instantly, eliminating the need to repeat manual calculations.

A simple example but you can draw your own conclusions as to how this applies to any logic flow.


4. Modularizing Code with Functions

Complex problems benefit from modular code, breaking down a task into discrete steps using functions enhances readability, reduces errors, and makes maintenance simpler.

Code that's obvious today becomes cryptic in six months when you forget what you did. Modular functions with clear names serve as documentation for your future self.

Case Study: Structural Loads

Imagine you’re calculating structural loads on multiple members, each with different types of forces. You could structure your code using functions for each part of the process:

Keep reading with a 7-day free trial

Subscribe to Flocode: Engineering Insights 🌊 to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 James O'Reilly
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share