Real-world case: The units that crossed a function boundary
1998 to 1999 · Spacecraft navigation software
The Mars Climate Orbiter was launched in December 1998 to study the weather and climate of Mars. Its thrusters were fired from time to time to keep the spacecraft pointing correctly, and a ground program had to report how large each firing was.
The written interface between the teams required that program, called SM_FORCES, to give its results in newton-seconds, a metric unit. It gave them in pound-force seconds instead. The navigation software took the numbers as newton-seconds, so it underestimated the effect of the thruster firings by a factor of 4.45, the number of newtons in one pound-force.
The error built up over nine months. On 23 September 1999 the spacecraft approached Mars on a lower path than planned, and contact was lost. The investigation board noted that odd data had been seen earlier and discussed informally, but that the anomalies slipped through the cracks.
How the case shows what you learned in this module
| Module idea | In the case |
|---|---|
| Inputs and outputs need units | The number was right; the unit was not, and the receiving code could not tell. |
| A function boundary is where errors cross | The output of one piece became the input of another with no check. |
| Name or document the unit | A name such as impulse_newton_seconds, or a docstring, makes the mismatch visible. |
| Test with a hand-worked case | One value checked against a known answer would have shown a factor of 4.45. |
Workplace takeaway. When one function hands a number to another, state the unit, convert once at the edge, and test the pair with a case you can work out by hand.
Sources: NASA Mars Climate Orbiter Mishap Investigation Board, Phase I Report (November 1999); NASA Safety Message, System Failure Case Studies, Vol. 3 Issue 5 (August 2009); Dr. Dobb's, Failure Analysis
Discussion prompt
20 minutes. Think of a calculation or rule you repeat in your work or study. In 150 to 200 words, describe the function you would write: its name, its parameters with units, what it returns and one test case you could work out by hand.
Private: nothing you type here is sent or stored.
This course has no discussion board yet, so the prompt is a private reflection.
Recap
- def defines a function and name() calls it; the body runs only on a call.
- Parameters take values in, return sends a result back, and print only shows.
- Local variables disappear after the call; pass values in rather than rely on globals.
- Small functions with one job, stated units and hand-worked tests are safe to combine.
Flashcards: 12 workplace review cards
Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.
Card 1 of 12 · What is a function?
A named block of code you can run whenever you need it. A tax calculation, written once and used on every sale, is a function.
Card 2 of 12 · How do you define and call one?
Define with def name(): and an indented body; call with name(). The body runs only on a call.
Card 3 of 12 · Why use a function instead of copying code?
One place to fix and to test. If the rule changes, every use is corrected. Copies drift apart and one is always missed.
Card 4 of 12 · What is a parameter?
A name in the brackets of the def that receives a value, such as price in def with_tax(price, rate).
Card 5 of 12 · What is an argument?
The actual value you pass when you call, such as 200 in with_tax(200, 0.1). Arguments match parameters by position.
Card 6 of 12 · What does return do?
It ends the function and hands a value to the caller, so you can store it or use it in another calculation.
Card 7 of 12 · What is the difference between print and return?
Print only shows text on the screen. Return gives the value to the program. A function that only prints gives back None.
Card 8 of 12 · What is a local variable?
A name created inside a function. It exists only while the function runs and is gone afterwards.
Card 9 of 12 · What is a NameError?
Python cannot see the name you used, for example a local variable used after the function has ended. Return the value you need instead.
Card 10 of 12 · Why pass values in rather than use globals?
A function that gets everything from its parameters can be understood and tested alone. A hidden global can change without warning.
Card 11 of 12 · What makes a good function?
One clear job, a name that says it, and a docstring stating inputs, output and unit. A function that calculates a grade should not also print a report.
Card 12 of 12 · How do you test a function?
Call it alone with a case you can work out by hand, then with the edges: just below, at and just above a boundary. Test pieces before combining them.
Module notes
Key terms. Function: a named block of code. Parameter: a name in the def. Argument: a value passed in a call. Return value: what a function gives back. Local variable: a name that exists only during a call. Docstring: a short description of a function. NameError: a name that is not visible.
Rules and formulas. one function, one job; convert units once, at the edge; test each function alone with a case worked by hand.
Limits of this module. Examples use Python 3. Optional parameters, returning several values and the global keyword are left for further study. Run each example to confirm the output shown.
Next
Continue to Module 6: Working with Files and Data. You can also return to the course overview or the University Track.
References and further reading
- Python Software Foundation. The Python Tutorial and The Python Language Reference (documentation for Python 3), available from python.org.
- Specialty Digest encyclopedia: Programming Language, Algorithm, Object-Oriented Programming and Binary Search.