''' Purpose: try out functions ''' import harb # import access to module harb # import statements are necessary to access functions beyond the built-in # we've imported url and random in the past, now we'll write our own modules and import those # just like with url.py, we must have our module files in the same folder as our programs # if you have organization issues in pycharm please see a TA! n1, n2 = 3, 14 # get some values for function exploration n3, n4 = 15, 92 n5, n6 = -6, 53 t1 = harb.add( n1, n2 ) # invoke harb.add() with arguments n1 and n2 # return value used to set t1 # these are function invokations, we grab out the add() function from the harb.py module # when the add() function is running, it needs to be flexible. it cannot count on n1 being the same every time # we need to know how many pieces of information we're being given and how to use them # using a function v. making a function # using -> invoking -> give arguments in the parentheses (add takes two arguments in each invokation) # a copy of those arguments are handed into the function # the function does not know the memory locations of the arguments and cannot change them # this differs from language to language # arguments could be numbers, strings, variables, literals, functions # if we handed 2 strings to the harb.add() function, it would concatenate them # making -> defining -> set up with parameters: special variables that are initialized when function is invoked # parameters are similar to loop variables, they are stand ins for the arguments that are later passed in # when we call a function, we run the code of the function ( ex. harb.add( n1, n2 ) ) # we leave the original program and go to the add() function, it has control over the program flow # while the function is in charge, we can access variables from harb.py, but not the variables in inger.py # copies of n1 and n2 are passed into the function, the function returns # the main code is paused until we reach a return value, then t2 = harb.add( n3, n4 ) # invoke harb.add() with arguments n3 and n4 # return value used to set t2 # we have to hand add() exactly two things (or arguments) or it will get mad at us # all functions do something, but not all functions hand something back ( think print() and append() ) # "handing back" is returning in python. this is not the same as using print() # print() displays output # returning hands back relevant information to the program that called (or invoked) the function # when you write functions in CS 1112, you might return every time, but you will NOT use print() statements in functions # printing is for the user, returning is for the code. we want to capture the output and use it, so we return # a function will do the same thing every run print( "add(", n1, ",", n2, "):", t1 ) # print first add() result print( "add(", n3, ",", n4, "):", t2 ) # print second add() result print() i1 = harb.negate( n5 ) # invoke harb.negate() with argument n5 # return value used to set i1 i2 = harb.negate( n6 ) # invoke harb.negate() with argument n6 # return value used to set i2 print( "inverse(", n5, "):", i1 ) # print first inverse() result print( "inverse(", n6, "):", i2 ) # print second inverse() result