EE1021 Lecture 3 Functions and Modules Worksheet
Topics: function definitions and calls, parameters and arguments, return values, local scope, default arguments, decisions and loops inside functions, and modules
Workflow: predict or outline first, implement in Spyder, test with normal and boundary inputs, and explain at least one correction you made.
Core work: A1–A2, B1–B2, C1–C2, D1, and E1. Extension work: D2, E2, and Section F.
Use the submission template EE1021_Lecture3_Functions_Worksheet_Template.py. Do not use lists, tuples, dictionaries, or sets in this worksheet.
Section A – Definitions, Calls, and Arguments
Trace the program without running it first.
def scale_reading(reading, factor): return reading * factor first = scale_reading(4, 2) second = scale_reading(first, 0.5) print(first, second)Identify the parameters, the arguments in each call, and the exact output.
Implement
rectangle_area(length, width)with a short docstring. It must return the area rather than print it. Show calls for a5 × 3rectangle and a2.5 × 4rectangle.
Section B – Return Values and Scope
Repair this function so that
answerbecomes10while the finalprint()remains in the main code.def double_value(value): print(value * 2) answer = double_value(5) print("answer:", answer)In one sentence, explain why the original function assigns
Nonetoanswer.Predict both output lines and explain why the two values named
countdo not remain equal.def increase(count): count = count + 1 print("inside:", count) return count count = 3 new_count = increase(count) print("outside:", count, new_count)
Section C – Functions with Decisions and Loops
Implement
classify_current(current_ma, limit=18)usingif/elif/elseor equivalent return paths:- negative current →
"Invalid" current_ma <= limit→"Safe"limit < current_ma <= limit + 5→"Warning"- above
limit + 5→"Overload"
Test
-1,18,19,23, and24with the default limit.- negative current →
Implement
sum_multiples(limit, divisor). It must use aforloop to return the sum of positive multiples ofdivisorup to and includinglimit.Required tests:
sum_multiples(12, 3) # 30 sum_multiples(10, 2) # 30 sum_multiples(4, 5) # 0Explain why the third call returns
0.
Section D – Default Arguments and Composition
Implement:
def calibrate_voltage(raw_voltage, offset=0.05): """Return raw_voltage corrected by offset."""Test one call that uses the default and one that overrides it. State which call uses which value.
Implement
voltage_status(voltage)using the Lecture 3 lab boundaries, then write main code that:- calibrates
raw_voltage = 4.72 - passes the returned calibrated value into
voltage_status() - prints the raw value, calibrated value, and status
Keep input/output in the main code rather than inside the two calculation functions.
- calibrates
Section E – Standard and Custom Modules
Import
mathand implement:def impedance_magnitude(resistance, reactance): """Return sqrt(resistance**2 + reactance**2)."""Verify that
(3, 4)returns5.0and(5, 12)returns13.0.Move
calibrate_voltage()andvoltage_status()into a file namedsensor_tools.py. In a separatemain.py, import the module and call both functions using thesensor_tools.prefix. Explain why the prefix is useful.
Section F – Mini Project: Sensor Report
Create a two-file program that produces a report for one temperature measurement.
temperature_tools.py must define:
celsius_to_fahrenheit(celsius)temperature_status(celsius, lower=15, upper=30)returning"Low","Normal", or"High"
main.py must:
- define or read one Celsius temperature
- call both module functions
- print the Celsius value, Fahrenheit value, and status
- demonstrate one normal case and two boundary or outside-range cases
Before coding, write a four-line outline identifying each function’s parameters and returned value.
Reflection
- Which mistake is easier for you to recognize now: forgetting to call a function, or printing instead of returning?
- Which boundary test found the most useful problem?
- What does placing functions in a module improve?