# Chapter4_basics.py # Basics about functions in OOP ''' Types of Functions myFunctionName() Empty Function "no passed data to this function" myFunctionName(param1, param2, ...) Parameterized Function "data is passed to this function myFullName(firstname, lastname) myFunctionName(param1 = "value") Default or Query String Function myFunctionName(firstname, lastname, state="Florida") If state is NOT passed, it defaults to "Florida", if the state IS passed, it uses the passed value myFunctionName('Bob','Smith') -> state="Florida" myFunctionName('Bob','Smith','Iowa') -> state="Iowa" ''' # Difference between functions and methods (level 2 when using class objects) ## Functions return value to themself ## Methods return value to something else ## How you handle a return? do you use the return keyword # Always 3 parts to a function (it) ## Whenever you are stuck -> Ask yourself these 3 things? ''' 1. Did I define it? 2. Did I give it value? 3. Did I return it? ---> call whatever it is ''' # Functions isolate blocks of code to either act as a behavior and/or give structure to data # We call the isolated block of code "aka, what is indented" a scope of the function # indentation determines the scope of the operation # Basic function that is empty # define the function with the def keyword def myCalculator(): x = 7 y = 3 z = x + y print(z) # function # method # return z # define a function that accepts 2 args (x,y) def myCalc1(x,y): """ docstring -> This calculator function will add 2 numbers and print the result. The function accepts 2 params for x and y. """ z = x + y print(z) # Define a function that accepts 3 parameters for first and last name and age. We need a print line using the print() that has the users full name and age in 10 years. Then call the function in your program. The values are from a user input() def fullname_age(fname, lname, age): # new obj in memory -> join fullName = fname + ' ' + lname # fullName1 = f"{fname} {lname}" string1 = f'Full Name: {fullName} Age: {age} Age in 10 Years: {age + 10}' # assigning value to var name string1 print(string1) # calling a variable named string1 from memory # call the function myCalculator() # 10 print("-------") x1 = int(input("Enter value for x1: ")) # 20 y1 = input("Enter value for y1: ") # 40 myCalc1(x1,int(y1)) # 60 # Calls the function, passes the params and prints the output fname = input("Enter your first name: ").title() lname = input("Enter your last name: ").title() age = int(input("Enter your age: ")) fullname_age(fname, lname, age) # Fullname: Bob Smith Age: 21 Age in Ten Year: 31 # EOF Chapter4_basics.py