56
1.15. SUMMARY
51
(h) format(expression, formatString) [1.14.1]
If expression is numeric, the format string can be in the form ’.#f’, where the # gets replaced
by a nonnegative integer, and the result is a string with the value of the expression rounded
to the specified number of digits beyond the decimal point.
(16) Functions defined by a user:
def functionName ( parameter1, parameter2, and so on) :
consistently indented statement block, which may include a return statement
(a) There may be any number of parameters. The parentheses must be included even if there are
no parameters. [1.11.4]
(b) When a function is first defined, it is only remembered: its lines are not executed. [1.11.2]
(c) When the function is later called in other code, the actual parameters in the function call are
used to initialize the local variables parameter1, parameter2, and so on in the same order as
the actual parameters. [1.11.4]
(d) The local variables of a function are independent of the local names of any function defined
outside of this function. The local variables must be initialized before use, and the names lose
any association with their values when the function execution terminates. [1.11.8]
(e) If a return statement is reached, any further statements in the function are ignored. [1.11.6]
(f) Functions should be used to :
(i) Emphasize that the code corresponds to one idea and give an easily recognizable name.
[1.11.2]
(ii) Avoid repetition. If a basic idea is repeated with just the data changing, it will be easier
to follow and use if it is coded once as a function with parameters, that gets called with
the appropriate actual parameters when needed. [1.11.4]
(iii) It is good to separate the internal processing of data from the input and output of data.
This typically means placing the processing of data and the return of the result in a
function. [1.11.4]
(iv) Separate responsibilities: The consumer of a function only needs to know the name,
parameter usage, and meaning of any returned value. Only the writer of a function
needs to know the implementation of a function. [1.11.7]
(17) Modules (program files)
(a) A module may start with a documentation string. [1.9.4]
(b) Define your functions in your module. If the module is intended as a main program called only
one way, a convention is make your execution just be calling a function called main. [1.11.3]
(c) Avoid defining variable outside of your functions. Names for constant (unchanging) values are
areasonable exception. [1.11.9]
(18) Documentation String: A string, often a multi-line (triple quoted) string that may appear in two
places:
(a) At the very beginning of a file: This should give overall introductory information about the
file [1.9.4]
(b) As the very first entry in the body of a function: This should describe: [1.12.1]
(i) The return value of the function (if there is one)
(ii) Anything about the parameters that is not totally obvious from the names
(iii) Anything about the results from the function that is not obvious from the name
(19) Programming Patterns
(a) Input-calculate-Output: This is the simplest overall program model. First obtain all the data
you need (for instance by prompting the user for keyboard input). Calculate what you need
from this data. Output the data (for instance to the screen with print functions). [??]
(b) Repetitive patterns: These patterns are all associated with loops. Loops are essential if the
number of repetitions depends on dynamic data in the program. Even if you could avoid a
loop by repeating code, a loop is usually a better choice to make the repetitive logic of your
program clear to all.
(i) Exact repetition some number of times: If the number of time to repeat is n:
for i in range(n):
actions to be repeated