''' Purpose: try out functions ''' # This is a program (a tester for the module harb) # Recall that modules / libraries (interchangeable terms) contain function definitions # Function definitions look like this # def function_name( parameters ): # function statements # return statement (possibly) # WE call functions in one of two ways: # variable = function_name( arguments ) <- arguments are passed in as the values for parameters # function_name(arguments) <- This just calls the function if the function maybe doesn't return anything (changes something / # executes function and moves on) # If the function returns a value, that return value is stored in the variable on line 12. # This file contains FUNCTION CALLS (invocations) of functions from the module harb # harb has the function definitions! import harb # import access to module harb n1, n2 = 3, 14 # get some values for function exploration n3, n4 = 15, 92 n5, n6 = -6, 53 # Function calls: # module_name.function_name( arguments ) # add has parameters a and b # n1 and n2 are the arguments for a and b # a takes on the value n1 # b takes on the value n2 # IMPORTANT the return value is stored in the variable that the function call is assigned to! # ^^^^^^^^^ Please remember this! # t1 stores the return value of the function call harb.add( n1, n2 ) # So t1 would be 17 since 17 is the return value from the function t1 = harb.add( n1, n2 ) # invoke harb.add() with arguments n1 and n2 # return value used to set t1 t2 = harb.add( n3, n4 ) # invoke harb.add() with arguments n3 and n4 # return value used to set t2 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 # i3 = harb.negate() # This will not work! You didn't give it anything as an argument for x! # print( i3 ) # NUMBER OF ARGUMENTS HAS TO BE THE SAME AS THE NUMBER OF PARAMETERS. # Don't give too many arguments or too little! s1 = 'happy' s2 = 'today' a3 = harb.add(s1, s2) # 'happytoday' # Will this add function still work if you give it two strings instead of numbers? # Yes! Because the operator + adds for numbers but does string concatenation for strings # print( a3 ) a1 = [3,1,4] a2 = [1,5,9,2,6] a4 = harb.add(a1, a2) # print( a4 ) # print( result ) # The result variable is a local variable only known within the scope of the function # harb so inger doesn't know what result is. # print( harb.add.result ) # Same thing ^ # Local variables are variables defined in the scope of the function. # Likewise, you can't do like # print(a) # or print(b) # inger.py doesn't know what a and b are! # Will this add function still work for lists? # Yes! Because the + operator combines two lists # x1 = 3 # x2 = 'apple' # a5 = harb.add( x1, x2 ) # print( a5 ) # This function will NOT work for a string and an integer because you can't use # + for a string and integer # An important thing to remember is that we don't define the type of the parameters being passed in # We can pass in strings, lists, numbers for a and b and Python will just evaluate the result # based on whether or not it can execute the statements with the arguments you gave it # It's based on legal Python but parameters are just placeholders for arguments. # You don't define the type of the arguments beforehand. # In Python you would do like # def function_name( a, b): # In Java, types of parameters ARE defined (but you don't need java for this class that's 2110) :) # public void function_name( int a, int b)