Class 25 — Monday March 23
Reaching an understanding with functions
Functional living — Problem-solving strategy — Embrace this future
Look both ways
Agenda
- Another try at functions
Starting discussion
- Slide show available at bottom of this page
Examples
- Module harb.py
- Program / tester inger.py
- Module olio.py
- Program / tester use_olio.py
- Module uva.py
- Program / tester rotunda.py
Homework 22
- Module double.py
- Program / tester threat.py
To do
- Look over artifacts
- Look at Hands on Python
- Review this page
- Preview next class activities activities
- Look at function epistle
- Two www.python.org resources should be examined now and later.
- The Python Tutorial includes a section on functions. I recommend before going there to thoroughly review the discussion presented here.
- Python enhancement proposal 008 (PEP 008) includes recommended practices for naming modules, functions, and variables. The recommendations suggest that function and variable names are as a rule written in snake_case; that is, lower-case words joined by underscores. Module names are expected to be short, lower-case, and no separation between words. I will add that function names are often action or verb phrases; e.g.,
input_integer()
.
Module harb.py
Function
sum()
- Has two integer parameters
a
andb
. Returns their sum.
- If the following code segment from program
inger.py
is executed
import harb
n1, n2 = 3, 14
n3, n4 = 15, 92
t1 = harb.sum( n1, n2 )
t2 = harb.sum( n3, n4 )
print( "sum(", n1, ",", n2, "):", t1 )
print( "sum(", n3, ",", n4, "):", t2 )
then the output should be
sum( 3 , 14 ): 17
sum( 15 , 92 ): 107
Function
inverse( x )
- Has one integer parameter
x
. Returns the additive inverse ofx
; i.e,-x
n5, n6 = -6, 53
i1 = harb.negate( n5 )
i2 = harb.negate( n6 )
print( "inverse(", n5, "):", i1 )
print( "inverse(", n6, "):", i2 )
then the output should be
inverse( -6 ): 6
inverse( 53 ): -53
Modules
-
A module is a named collection of functions
- Module name required to be a legal identifier
- The functions of a module are stored in a Python file (
.py
) that matches the module name
- All modules we develop will be kept in your CS 1112 class folder
- Access to a module capabilities is via importing
import module_name
Functions
-
A function is a named block of code to carry out some specific task
- Function name required to be a legal identifier (just like variables are)
- Functions can have special variables called parameters to store start up values
- Access to a module capabilities is via invocation
module_name . function_name ( arguments )
Arguments are start up values copied/passed to the function for initializing parameters
- Functions can produce information that is handed back via a return statement
return return expression
- For functions without return returns Python supplies the value
None
Function syntax
- A 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 parenthesized list of variable names (identifiers)
- The function_name along with the parameters form the function header
- The colon
:
is a separator of the function header from the function and its action
- The header comment (docstring) describes the function
- The action is a non-empty block of statements that run when the function is invoked
Function invocation
- Checks whether the number of arguments in the invocation and parameters in the definition match
- No match: an execution error occurs
- Match: arguments are evaluated (values are determined)
- Sets aside some computer memory for carrying out the function
- That memory is called an activation record or a frame
- Stores values of the parameter variables
- The parameter variables are initialized with the argument evaluations
- Stores the values of other variables needed to perform its task
- The function’s code runs; i.e., it has the flow of control
- When the function completes, the flow of control goes back to the code that did the invocation, and it is that code now continues being executed
- If a
return
statement is reached, the return value is determined
- If there is an expression following keyword return, its value is the return value
- If there is no expression following keyword return, the return value is
None
If the code complete without reaching a
return
statement, the return value is set toNone
When the return value is set, the execution of the function stops and
- The flow of control goes back to the statement that did the invocation
- The value of the invocation – its evaluation – is the return value
Taxonomy
- The two main kinds of functions are
- Functions that have parameters and return values
- Functions that have parameters and have
None
as the 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 the return value
Implementing functions
- When designing a function we need to
- Determine what information is needed upon startup
- Determine what information is to be produced
- Provide an algorithm for going from one to the other
Module olio.py
Function
voting_age()
- Does not take any parameters. Returns an integer indicating how old you need to be to vote. For example, the following code segment initializes both x and y to the integer
18
.
x = olio.voting_age()
y = olio.voting_age()
Function
has_blanks( s )
- Has one parameter
s
. Returns whethers
contains at least one blank character; i.e., returnsTrue
ifs
contains a blank, and returnsFalse
otherwise. For example, the following code segment initializesb1
andb2
toTrue
andFalse
respectively.
x = 'CS 1112'
y = 'the_aarvark_said_arf_arf'
b1 = olio.has_blanks( x )
b2 = olio.has_blanks( y )
Function
great_seal()
- Does not have any parameters and does not return a value. The function prints text from the Great Seal of the United States, i.e., E Pluribus Unum.
- The code segment
olio.great_seal( )
print()
olio.great_seal( )
print()
produces as output
E Pluribus Unum
E Pluribus Unum
Function
a_ing( n )
- Has one parameter
n
. The function does not perform a return, instead it printsn
lines of output. The first line prints a single'a'
, the second line prints'aa'
, the third line prints'aaa'
, and so on. The string operator*
should prove useful. For example usage, the code segment
olio.a_ing( 5 )
print()
olio.a_ing( 1 )
print()
olio.a_ing( 3 )
produces as output
a
aa
aaa
aaaa
aaaaa
a
a
aa
aaa
Module uva.py
- Let's implement a worthwhile module with four functions
- A function with no parameters that does not perform an explicit return;
- A function with parameter(s) that does not perform an explicit return;
- A function with no parameters that returns something of value;
- A function with parameter(s) that returns something of value;
- Program rotunda.py
- Let's implement along the way a simple tester of module
uva.py
Slide show
© 2020 Jim Cohoon | Resources from previous semesters are available. |