Class 25 — Wednesday March 20

Form with function

Why do I clang so — Is that to be my function — The Paw Patrol knows

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


Look both ways


Agenda


Druthers


Downloads


Changing our modus operandi


Nota bene (n.b.)


Why have functions


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 '''

  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 ( : ) 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. Note, 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.

None



Taxonomy


Staying within the law of scope

import url

page = url.get_text( 'http://www.cs.virginia.edu/~cs1112/' )

print( url.get_text.text )


Module olio.py

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( )

print()

olio.great_seal( )

print()

produces as output

E Pluribus Unum

E Pluribus Unum


Function a_ing( n )

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


What's next


To do


pitch perfect poster