-
If compute a number, a string or a boolean and give it back → return the value and print nothing; the caller decides what to show
Example: def average(marks): return sum(marks) / len(marks), so average([8, 6]) is 7.0
-
If display a report, a table, a message → a function that prints and returns None, calling the computing ones; it lives at the outer layer, often main()
Example: def show(record): print(report_line(record)), where report_line RETURNS the string
-
If modify the list you are given: sort it, remove from it, add to it → work in place, return None, and say so in the docstring
Example: def drop_negatives(marks): walks marks[:] and removes 2 values from [8, -2, 6, -1]
-
If build a new list from the one you are given → return the new list and leave the argument untouched, with a different name from the in place version
Example: def without_negatives(marks): return [m for m in marks if m >= 0], 2 elements kept
-
If answer yes or no about ALL the elements → return False at the first counterexample, return True after the loop
Example: all_even([2, 4, 7]) returns False at the 7, all_even([2, 4, 8]) reaches the final return True
-
If keep a value from one call to the next: a counter, a running list → no global, no default list: the value comes in as a parameter and goes out in the return
Example: def register(mark, count): return count + 1, and the caller writes count = register(8, count)
a parameter that may be omitted takes None as default, and the body builds the list
-
If a function needs another function to do its job: a key, a rule to apply → pass the function by its name, without brackets
Example: sorted(names, key=len) passes len; key=len(names) passes the number 3 and raises TypeError
If no branch fits in one sentence, the specification describes two jobs: cut the function in two. A global statement is never the branch; in COMP 202 it costs style marks and makes the function untestable.