Functions: an Introduction

Using definitions to write better style programs

 

###################################
#  Re-write this program using functions 
#  happy1_GE.py
#  
#  Mrs. Elia
#  python 3.9
#  10/8
#
###################################

name = input("what is your name? ")
print("Happy Birthday to you!")
print("Happy Birthday to you!")
print("Happy Birthday, dear ", name + "!")
print("Happy Birthday to you!")

##/My_Python_Programs/Unit2/happy1_GE.py
##what is your name? grace
##Happy Birthday to you!
##Happy Birthday to you!
##Happy Birthday, dear grace!
##Happy Birthday to you!
##>>> 

Let’s re-write it with functions:

###################################
#
#  happy2_GE.py
#  Using functions to design your program
#  Mrs. Elia
#  python 3.9
#  10/8
#
###################################

def happy():
   print("Happy Birthday to you!")

def sing(person):
   print("Happy Birthday, dear ", person + "!")
   
def main():
   name = input("what is your name? ")
   happy()
   happy()
   sing(name)
   happy()

main()



##/My_Python_Programs/Unit2/happy2_GE.py
##what is your name? grace
##Happy Birthday to you!
##Happy Birthday to you!
##Happy Birthday, dear grace!
##Happy Birthday to you!
##>>>