Concepts / Introduction to Functions and Return Values

Introduction to Functions and Return Values

The central pattern is to maintain one variable that represents the extreme value found so far, then update it only when a new value is more extreme.

  • Programming

Why Extreme Values Matter

Many programs need to find an extreme value in a collection of data. A weather application might find the highest temperature recorded during a week. A sales system might identify the lowest-priced item in inventory. A fitness tracker might find the maximum heart rate during a workout. These tasks follow the same basic idea: examine each value and remember the largest or smallest value seen so far.

Key point

The central pattern is to maintain one variable that represents the extreme value found so far, then update it only when a new value is more extreme.

Tracking the Largest Value

For a maximum loop, the tracking variable represents the largest value encountered up to the current iteration. The comparison has two parts: largest is None handles the empty starting state, and itervar > largest checks whether the current value is larger than the saved value. The saved value changes only when one of those conditions is true.

largestNonelargest4largest9largest9largest11compare 4compare 9compare 2compare 11
How does the largest-so-far variable change as each value is compared?
Worked example

Finding a maximum step by step

Apply the largest-so-far pattern to the sequence 4, 9, 2, 11.

  1. 1
    Start

    Set largest to None because no sequence value has been examined yet.

  2. 2
    Read 4

    largest is None, so largest becomes 4.

  3. 3
    Read 9

    9 is greater than 4, so largest becomes 9.

  4. 4
    Read 2

    2 is not greater than 9, so largest remains 9.

  5. 5
    Read 11

    11 is greater than 9, so largest becomes 11.

Result

The largest value found is 11.

Writing the Maximum Pattern

python

The initialization occurs before the loop. Each iteration examines itervar. If largest is still None, the current value becomes the first saved value. Otherwise, the current value replaces largest only when it is strictly greater. A value equal to the current maximum does not change the tracking variable.

Tracking the Smallest Value

The minimum pattern has the same structure as the maximum pattern. The variable begins as None, the first value fills the empty state, and later values are compared with the saved extreme. The only comparison change is the operator: a minimum loop uses itervar < smallest instead of itervar > largest.

smallestNonesmallest8smallest3smallest3smallest1compare 8compare 3compare 6compare 1
How does the smallest-so-far variable change as each value is compared?
python
Worked example

Finding a minimum step by step

Apply the smallest-so-far pattern to the sequence 8, 3, 6, 1.

  1. 1
    Start

    Set smallest to None because no sequence value has been examined yet.

  2. 2
    Read 8

    smallest is None, so smallest becomes 8.

  3. 3
    Read 3

    3 is less than 8, so smallest becomes 3.

  4. 4
    Read 6

    6 is not less than 3, so smallest remains 3.

  5. 5
    Read 1

    1 is less than 3, so smallest becomes 1.

Result

The smallest value found is 1.

Starting with None

Definition

None marks the extreme variable as empty before the loop has examined any value. On the first iteration, the condition involving None allows the first sequence value to become the initial maximum or minimum. After that, ordinary greater-than or less-than comparisons control updates.

largestNonelargest4largest11first valuelarger value found
How does an extreme variable change from None to the first value and then to a valid extreme?

Using Functions for Extreme Values

Python provides the built-in max() and min() functions for finding extreme values. In real-world code, use these functions when the task is simply to find the maximum or minimum. They are more concise, faster, and less error-prone than writing the loop yourself. The manual loop remains important because it exposes the underlying algorithm, supports debugging, and transfers to languages that do not provide built-in extreme functions.

python
valuessequencemax()extreme-finding functionhighestmaximum valuesequencecomputed value
How does an extreme value computed by a function become available to the code that uses the result?

Mistakes in Extreme Loops

  • Common mistake

    Initializing the tracking variable to an arbitrary value.

    What it looks like
    largest = 0
    Why it fails

    The chosen starting value may not represent an actual sequence value and can interfere with finding the correct extreme.

    Do this instead

    Initialize the extreme variable to None, then let the first value establish the starting point.

  • Common mistake

    Using the wrong comparison operator.

    What it looks like
    if smallest is None or itervar > smallest:
    Why it fails

    A minimum loop must replace the saved value only when the new value is smaller.

    Do this instead

    Use itervar < smallest for the minimum pattern.

  • Common mistake

    Updating the extreme on every iteration.

    What it looks like
    largest = itervar
    Why it fails

    The saved value would become the most recently examined value rather than the largest value seen so far.

    Do this instead

    Update only when the current value is strictly more extreme.

  • Common mistake

    Writing a manual loop when a built-in function is sufficient.

    What it looks like
    Use max() or min() for a direct extreme-finding task.
    Why it fails

    The manual version is longer and more prone to errors.

    Do this instead

    Prefer the built-in function in real-world code when no additional loop behavior is required.

Practice the Pattern

MEDIUM

For the sequence 12, 5, 18, 18, 7, trace the value of largest after every iteration. Then write the condition for a smallest-so-far loop and trace smallest for the same sequence.

Hints
  • Begin each tracking variable as None.
  • For largest, update only when the current value is greater than the saved value.
  • For smallest, update only when the current value is less than the saved value.
  • An equal value does not change the tracking variable.

Key Takeaways

  1. 1
    A maximum loop maintains the largest value seen so far and uses the greater-than operator.
  2. 2
    A minimum loop maintains the smallest value seen so far and uses the less-than operator.
  3. 3
    None marks the tracking variable as empty before the first sequence value is examined.
  4. 4
    The tracking variable changes only when a strictly more extreme value appears.
  5. 5
    Use built-in max() and min() for ordinary extreme-finding tasks, but learn the manual loop to understand the algorithm and debug it.

At a glance

  • Extreme-finding loops compare each value with the best value found so far.
  • Use None as the initial state so the first value can establish the maximum or minimum.
  • Maximum loops use >, while minimum loops use <.
  • A tracking variable remains unchanged when the new value is equal to or less extreme than the saved value.
  • Prefer max() and min() for straightforward real-world tasks, while understanding the manual pattern for algorithmic insight and debugging.

Related concepts

More concepts starting with I