Class 24 — Monday October 19
Reaching an understanding with functions
Functional living — Problem-solving strategy — Embrace this future
Look both ways
Agenda
- Continue function-based chrestomathics
Examples
- Module harb.py
- Program inger.py
- Module olio.py
- Program use_olio.py
Homework
- Module double.py
Why functions
- In large-scale 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.
- When done properly, functions provide an easier, clearer, and reusable way for the manipulation and calculation of values.
Module harb.py
Function add()
- Has two parameters
a
andb
. Returns the value of a + b
- If the following code segment from program
inger.py
is executed
import harb
n1, n2 = 3, 14
n3, n4 = 15, 92
t1 = harb.add( n1, n2 )
t2 = harb.add( n3, n4 )
print( "add(", n1, ",", n2, "):", t1 )
print( "add(", n3, ",", n4, "):", t2 )
then the output should be
add( 3 , 14 ): 17
add( 15 , 92 ): 107
Function negate( 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( "negate(", n5, "):", i1 )
print( "negate(", n6, "):", i2 )
then the output should be
negate( -6 ): 6
negate( 53 ): -53
Requirement
- All functions that we developed are to be stored in your CS 1112 folder.
Terminoloy
- A module is a named collection of functions.
- A module is stored in a Python file (
.py
) that matches its name.
- Module names are required to be identifiers.
- Access to a module capabilities requires for it to be imported.
import module_name
- A function is a named block of code to carry out some specific task
- Function names are required to be identifiers.
- Functions can have special variables called parameters to store start up values.
- To access a particular function in a module it must be invoked.
module_name . function_name ( arguments )
- The arguments are a list of start up values copied (passed) to the function so that it can carry out its task.
- The arguments are used to initialize the corresponding parameters of the function. The value of the first argument is used to intialize the first parameter, and so on.
- Parameter initialization occurs before the function stores to perform its task.
- 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
- For functions that end without return statements, Python supplies the value
None
- 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 parameters are a parenthesized list of variable names (identifiers)
- When invoked, the number of arguments being passed needs to be the same as the number of parameters.
- 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
- A function invocation causes some computer memory to be set aside for the function carrying its task. The memory is called an activation record or a frame
- The activation record stores values of the parameter variables.
- The actication record stores the values of other variables needed to perform its task.
def add( a, b ) : # function named add(), parameters named a and b
''' Returns value of a + b
'''
result = a + b # compute value of interest
return result # function hands back its result
- When a program starts up, its code is executed. We say that it has the flow of control
- When a program invokes a function, it temporarity passes the flow of control so tha code of the function can run.
- The code making up the action of a function is called a statement block.
- A function completes as soon as it reaches a
return
statement or when it finished executing its statement block, whatever happens first.
- If a
return
statement is reached, the return value is determined.
- If there is no expression following keyword
return
, the return value isNone
- If the code complete without reaching a
return
statement, the return value isNone
- Once a return value is set, the execution of the function immediately stops.
- The value of the function invocation – its evaluation – is the return value
- The flow of control goes back to the statement that did the invocation.
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
Designing functions
- Need to determine what information does a function need on startup.
- What information it is to 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
© 2020 Jim Cohoon | Resources from previous semesters are available. |