# webinar functions and methods in Python # You'll start getting to know functions in Ch 7 # functions should be modular and reusable # we define a function once and call it as many times as needed # defining a function def myFunction(parameter1, parameter2): # indented block for my function # print() a value.. print(parameter1, parameter2) # or return someValue instead, we'll do that next time myFunction("Hey", "There") myFunction("Goodbye", "... now") print(myFunction("Hey", "There")) # this will also output "None", b/c I printed a call to a function with no return # function definition, Gallant def squareThis(num): return num * num # RETURN means the function calls evaluate to and give back this new value # ... but you don't SEE a returned value, unless you print() the call itself print(squareThis(5)) # 25 # because we returned this time, we can use calls to this function in assignment statements x = squareThis(6) # 36... but not until I print it print(x) x = 10 + squareThis(7) # 59 print(x) # Note that if the function prints instead of returning, you can't use that for assignment or other expressions # Related: printing a call to a function with no return statement will show NONE in output # bad function from Goofus def squareSomething(num): num = 5 # don't do this! return num * num print("Gallant vs Goofus: ") print(squareThis(8)) # 64 print(squareSomething(8)) # 64? Nope it's value is 25 print(squareThis(3)) # 9 print(squareSomething(3)) # 9? Nope it's value is still 25 # Don't overwrite your parameters like Goofus! # Pay close attention on the exam to whether a question asks you # to RETURN or to PRINT() a value from the function. # If you see "None" in your output, you called print() when you should have returned! # You'll be getting to know various methods of common data types in Ch 8 and beyond # METHODS of a data type or "class" are just functions that belong to that type myList = [1, 2, 3, 4, 5, 3] # nameOfObject.method() count = myList.count(3) # this .count() is a list method, other types don't have it! print(count) # 2 print(myList.count(3)) # or I can print the call, because .count() returns something # help(list) # the built-in function help() will show me a help doc on a data type, including its methods