Class 25 — Monday March 23

Reaching an understanding with functions

Functional living — Problem-solving strategy — Embrace this future


Look both ways


Agenda


Starting discussion


Examples

 

 


Homework 22


To do


Module harb.py

Function sum()

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 )

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

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

import module_name

 


Functions

module_name . function_name ( arguments )

Arguments are start up values copied/passed  to the function for initializing parameters

return return expression

 

 


Function syntax

def function_name( parameters ) :

  ''' header_comment

  '''

  action


Function invocation

  • 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

If the code complete without reaching a return statement, the return value is set to None

When the return value is set, the execution of the function stops and

 


Taxonomy


Implementing functions


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


Module uva.py


Slide show

 


 
  © 2020 Jim Cohoon   Resources from previous semesters are available.