Class 23 — Monday October 21

Form with function

The task before us — Turn the abstract to concrete – Functions are at hand

Being functional — Is your mantra from now on — Embrace to your core

To be, not to be — That is the question I ask – The answer, to be

Look both ways




To do

GUI for photo manipulation homework screen grab

Changing our modus operandi

Why have functions

GUI for 15-square homework screen grab

Using functions

import module_name

module_name . function_name ( arguments )

Flow of control

Implementing functions

  • The information is called the parameter list. A parameter is a variable. Parameters are automatically initialized using information passed to it upon function startup.
  • 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

  • The algorithm is written as code. The code is known as a statement block or as the body of the function.

Function definition syntax and terminology

def function_name( parameters ) :

  ''' header_comment '''


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 ( : ) is a separator of the function header from the function statement block.

Although optional, the literal string ''' header-comment ''' is normally the first line following the function header. The header-comment is a documentation string or as it commonly abbreviated docstring. The header-comment docstring describes the operation of the function. A Python installation comes with tools (e.g., Sphinx) for automatically generating online documentation from a program's triple-quoted strings. Comments intended for online documentation are written as docstrings.

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. BTW, return is a keyword.

Function invocation syntax and terminology

module_name . function_name ( arguments )

import url

reply = input( 'Enter web file: ' )

link = reply.strip()

text = url.get_text( link )

Function invocation semantics — what happens under the hood

s = "Cole's law: marinate shredded cabbage in vinaigrette."

x = print( s )

print( x )

indicates even the print() function returns a return. The snippet output is

Cole's law: marinate shredded cabbage in vinaigrette.



Staying within the law of scope

import url

page = url.get_text( '' )

print( url.get_text.text )


Function voting_age()

x = olio.voting_age()

y = olio.voting_age()

Function has_blanks( s )

x = 'CS 1112'

y = 'the_aarvark_said_arf_arf'

b1 = olio.has_blanks( x )

b2 = olio.has_blanks( y )

Function great_seal()

olio.great_seal( )


olio.great_seal( )


produces as output

E Pluribus Unum

E Pluribus Unum

Function a_ing( n )

olio.a_ing( 5 )


olio.a_ing( 1 )


olio.a_ing( 3 )

produces as output










What's next

pitch perfect poster