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

  1. 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.

  2. Implement rectangle_area(length, width) with a short docstring. It must return the area rather than print it. Show calls for a 5 × 3 rectangle and a 2.5 × 4 rectangle.

Section B – Return Values and Scope

  1. Repair this function so that answer becomes 10 while the final print() 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 None to answer.

  2. Predict both output lines and explain why the two values named count do 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

  1. Implement classify_current(current_ma, limit=18) using if/elif/else or 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, and 24 with the default limit.

  2. Implement sum_multiples(limit, divisor). It must use a for loop to return the sum of positive multiples of divisor up to and including limit.

    Required tests:

    sum_multiples(12, 3)  # 30
    sum_multiples(10, 2)  # 30
    sum_multiples(4, 5)   # 0

    Explain why the third call returns 0.

Section D – Default Arguments and Composition

  1. 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.

  2. 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.

Section E – Standard and Custom Modules

  1. Import math and implement:

    def impedance_magnitude(resistance, reactance):
        """Return sqrt(resistance**2 + reactance**2)."""

    Verify that (3, 4) returns 5.0 and (5, 12) returns 13.0.

  2. Move calibrate_voltage() and voltage_status() into a file named sensor_tools.py. In a separate main.py, import the module and call both functions using the sensor_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

  1. Which mistake is easier for you to recognize now: forgetting to call a function, or printing instead of returning?
  2. Which boundary test found the most useful problem?
  3. What does placing functions in a module improve?