Class 24 — March 19
Form with function
Why do I clang so — Is that to be my function — The Paw Patrol knows
Practice makes perfect — Is more than a platitude — Take it to your heart
Look both ways
Agenda
- Start function-based chrestomathics
Gallery
Using functions
- Knowledge of what the function does and needs in order to be correctly used is paramount; i.e., carefully examine its documentation.
- If the function is not built-in, program access to the library (module) defining the function needs to be gotten by importing. A module name must be an identifier.
import module-name
- To make use of a function it needs to be invoked. A module-defined function invocation has form
module-name . function-name ( arguments )
- The module-name is the name of the
.py
file containing the definition of function function-name(). A function name must be an identifier.
- Operator
.
is the selection operator. It indicates that a component of the indicated module is to be accessed.
- The function-name is the name of the function being invoked.
- The ( arguments ) are a list of values that are passed to the function so that it can carry out its task.
Flow of control
- When a function is invoked, Phython passes control over to the function and it is now its code that is running. When the function completes, control is passed back to the code that contained the invocation, and its code now continues being executed.
Implementing functions
- In digital problem solving, the component tasks are written as independent units of code. For clarity, code units are named so that there is no ambiguity as to what unit is being considered. In Python we call a named unit of code, a function. The name of function must be an identifier.
- We use functions to provide an easier, clearer, and often reusable way for the manipulation and calculation of values. In designing a function we need to determine
- What information is needed to be given to the function upon startup;
- The information is called the parameter list. A parameter is a variable. Parameters are automatically initialized using information passed to it upon function startup.
- What information it is to produce.
- A function typically produces a value that is returned to the code that invoked it. Python provides the
return
statement for this purpose.
return function-result
- Provide an algorithm for going from one to the other.
- The algorithm is written as code. The code is known as a statement block or as the body of the function.
Function invocation syntax and terminology
- A typical Python function definition looks like the following
def function-name
(
parameters)
:
'''
header-comment'''
action
- The keyword
def
signals that a function is being defined.
- The function-name is an identifier that names the task.
- The parameters are a list of variable names (identifiers). The values of the parameters are the values needed by the function upon start up. The function-name along with the parameters form the function header.
- The colon separates the function header from the function statement block.
- The header-comment is referred to in Pythonics as a doc string and describes the operation of the function.
- The action is the block of statements to be executed when the function is called upon. The statements can include a
return
statement(s) that specifies the value to be associated with the function’s usage (return
is a keyword).
Functions
- The two main kinds of functions are:
- Functions that have parameters and return values
- Functions that have parameters and have
None
as a return value
- There can also be:
- Functions that do not have parameters, but do return values
- Functions that do not have parameters and have
None
as a return value.
To do
- Use CodeBat for function practice
- Look over artifacts
Warning: fopen(/l/cs1112/public_html/defs/htm/181-trailer): Failed to open stream: No such file or directory in /l/cs1112/public_html/defs/php/read.php on line 5
Unable to open file!