Give a reusable calculation a clear input and output.
Reading a message identifies its value, but a raw sensor value may still need a physical interpretation. We now put one conversion into a function we can reuse.
A teaching force sensor produces 0.5 V at 0 N and 4.5 V at 100 N. Assume its response is linear between those calibration points.
Start with the problem
Try this first.
Mark the two points on a sketch. Predict the force at 2.5 V. Then decide whether the same evidence establishes the force at 5.0 V.
Why this week's tool?
A function receives voltage and returns force using the calibration. Its name, parameters and return value make that calculation available without copying it everywhere.
By the end: Explain the function’s input, output and calibrated range, and distinguish a calculated extrapolation from a supported measurement.
The idea behind the program
A function needs a contract
Define the input quantity and unit, output quantity and unit, and valid domain. This model accepts voltage in volts and returns force in newtons. The calibration is an assumption supplied for this particular teaching sensor.
Derive the conversion
Slope = (100 − 0) / (4.5 − 0.5) = 25 N/V. Subtract the 0.5 V offset before multiplying: force = (voltage − 0.5) × 25. Checking both endpoints detects a missing offset.
Return a value for the next step
print() sends text to the screen. return gives a value to the caller for checking or composition. Keep conversion and presentation separate. Our minimal function performs the arithmetic; the caller is responsible for recognizing out-of-domain inputs.
A short Python example
Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.
A function call creates a local computation using the arguments. return supplies a value to the caller; print sends text to the output display. A function can print a correct-looking number while returning None.
Draw or trace
Draw an arrow from the argument 20 into a Celsius-to-Fahrenheit function and an arrow carrying 68 back to result. Put printed text in a separate output box.
Predict before running. If the function only prints 68, can its caller use result + 1 as if result were 68?
Trace and explanation — after your prediction
The argument 20 is bound to the local parameter.
The expression 20 × 9 / 5 + 32 evaluates to 68.
return passes 68 to the caller; print alone displays it and an implicit return gives None.
The print-only version cannot supply the numeric value for the next calculation. Define the function contract in terms of inputs, returned result and side effects, not the appearance of the screen.
Change one thing. Call the conversion twice with different inputs. Explain why each call should depend on its argument rather than a previously stored global measurement.
Türkçe: print ekrana yazar; return değeri çağırana verir. Görünen çıktı ile sonraki hesaplamada kullanılacak değer farklıdır.
Examples and variations
Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.
The midpoint is an interpolation. It does not establish real sensor linearity.
02Offset check
0.5 V; compare correct formula with voltage × 25
Question: Which formula respects the zero-force calibration?
Correct: (0.5 − 0.5) × 25 = 0
Faulty: 0.5 × 25 = 12.5
Endpoint test exposes the missing offset
0 N; faulty formula reports 12.5 N
Test physically meaningful points, not only arbitrary values.
03Outside calibration
5.0 V; stated calibration ends at 4.5 V
Question: Is the arithmetic result sufficient evidence for a force claim?
Formula yields (5 − 0.5) × 25 = 112.5
5.0 V is outside the calibrated interval
Linear behaviour there has not been supplied
Arithmetic: 112.5 N; unvalidated extrapolation
Report the domain issue. Rejecting or flagging is a policy decision; silently clamping would hide it.
See the Colab code run
Interactive walkthroughs of Functions — Basics. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.
Work on it in Colab
Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.
Draw the line through the two calibration points and derive its slope with units.
Implement the function. Test both endpoints and the midpoint before formatting any output.
Replace return with print in a temporary copy and inspect what the caller receives. Restore return before composition.
Pass 5.0 V and describe the difference between a calculated number and a justified measurement. Write the missing domain policy.
Something to take away: The calibration derivation, a function contract, three reference checks and an explicit treatment of extrapolation.
Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.
Optional notes & guidance
My notes
Notes stay in this browser. Download a copy to keep them.
Using AI or working with a partner
Ask AI for the conversion function. Require the offset, slope units and domain to be explained. Test both calibration endpoints yourself; do not accept only the model's own generated tests.
You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.