Class 23 — Friday October 16

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


Look both ways


Agenda


Examples



To do


Slides


Program wc.py — proves my point about importance of functions

''' Purpose: determine number of lines, words, and characters in a user-specified

  web file.

'''

# import module for internet access

import url

# get the lines of text from a user-specified file

reply = input( 'Enter web file link: ' )

link = reply.strip()

contents = url.get_contents( link )

# count the number of lines, words, and characters in the contents

nbr_lines = contents.count( '\n' )

words = contents.split()

nbr_words = len( words )

nbc_chars = len( contents )

# display result

print( 'nl =', nbr_lines )

print( 'nw =', nbr_words )

print( 'nc =', nbc_chars )



Here we go

  • We will get to that likely the end of next week.

You are already experienced with functions and modules (e.g,, wc.py)


Module harb.py

Function add()

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 )

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



Modules

  • All modules we develop will be kept in your CS 1112 class folder

import harb

 

...

 


Functions

module_name . function_name ( arguments )

t1 = harb.add( n1, n2 )

t2 = harb.add( n3, n4 )

return return expression

 


Function syntax

def function_name( parameters ) :

  ''' header_comment

  '''

  action

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



Function invocation

t1 = harb.add( n1, n2 ) # good: correct number of arguments

t2 = harb.add( n3, n4 ) # good: correct number of arguments

t3 = harb.add( ) # bad: too few arguments

t4 = harb.add( n1, n2, n3, n4 ) # bad: too many arguments

  • 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

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

  • 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
  • The return value is set None
  • The flow of control goes back to the statement that did the invocation
  • The value of the invocationits evaluation – is the return value

 


Taxonomy


Implementing functions


What's next


Slide show




GUI for photo manipulation homework screen grab


GUI for 15-square homework screen grab


colored continental USA map

 


  © 2020 Jim Cohoon   Resources from previous semesters are available.