''' Purpose: introduce modules ''' # This is a module! A module / library contains function definitions. # We will import this module to use it in other programs/ tester program. # We call functions from this module using harb.function_name(arguments) # Remember that arguments fill in parameters to start up the function! # We have two functions below, sum(a,b) and negate(x) # Remember that functions are defined like so: # def function_name(parameters): # ''' Here's what the function does ''' # It's useful to have a docstring but not required. # function body # ALL FUNCTIONS HAVE PARENTHESES. # Remember that variables and functions are identifiers. # Unique names you as the coder give to the functions/variables to use them later on! # sum and negate are identifiers (names of the functions) - we will use these same names to call these functions # later! # All functions either return the explicit return value or None by default. # In sum, we have the explicit return value of total. # So ALLLLLL functions have a return value (what you say to return or None) !!! # print() is not the same thing as return! !!! # Definition uses def function_name(parameters) # Invocation/call uses module.function_name(arguments) # Order Matters: Arguments are passed into parameters in the order they're in. # sum(2,4) gives a 2 and b 4 and sum(4,2) gives a 4 and b 2. # 2 and 4 are arguments 4 and 2 are arguments def sum( a, b ) : # function name is sum(), parameters are a and b ''' Returns sum of a and b ''' total = a + b # compute value of interest return total # function hands back the total # Once the code executes the return statement, everything after it isn't even # looked at. # return 88 # print('Here I am') # If we don't have an explicit return value (return statement), Python # returns None! Remember t1 stores the return value of the function call. # What's the return value of sum with no return statement? It's None. # I repeat: ALL FUNCTIONS RETURN VALUES (what you say to return or None). def negate( x ) : # function name is negate(), parameter is x ''' Returns inverse of x ''' inverse = -x # compute value of interest return inverse # function hands back the inverse