''' Name: Email id: Purpose: develop programmer-defined functions ''' # function voting_age(): returns how old you need to be to vote; i.e. the # integer 18. # FUNCTION DEFINITION # def function_name( parameters ): # function body indented into the function # All functions have a return value! It's either an explicit return value like 18 for voting_age or # it returns None by default! def voting_age() : age = 18 return age # return ( This alone is returning None - Python default return value for a function with no explicit return value) # return 18 alone also works for this function! # return 19 # This code won't run because we already reached the return statement # print( age ) # This code is unreachable! Why? Because we already returned something! It's AFTER the return statement # If you are in a for loop within a function and you hit the return statement, the function is complete and terminates. # It will not run code afterwards once it reaches the return statement. # You can have a function without a return statement! It's just that the value that gets handed back # will be None! # def dontreturn(): # x = 18 # This function returns nothing so the return value is None! # function has_blanks( s ): returns a whether s contains at least one blank # character; i.e., returns True if s contains a blank, and returns # False otherwise. # WE DO NOT PUT INPUT STATEMENTS IN OUR FUNCTIONS!!!!!!! # THE PARAMETERS ARE THE WAY THAT WE PASS VALUES IN TO START UP OUR FUNCTIONS. # In function calls, we use arguments to start up the function by passing those arguments in as our parameters. # so s is the parameter and 'CS 1112' is the argument! def has_blanks( s ): # s is our parameter that we need to provide an argument for when we call the function # print( s ) number_blanks = s.count(" ") if ( number_blanks >= 1): return True else: return False # What's another way we could've implemented this function? # We can count the number of blanks and see if it's greater than 1 # We can also use the 'in' keyword to see if the blank is in the string! # if (number_blanks >= 1): # result = True # else: # result = False # return result # if (" " in s): # return True # else: # return False # function great_seal( ): prints the string 'E Pluribus Unum' def great_seal(): # NO PARAMETERS output = "E Pluribus Unum" print( output) # function a_ing( n ) prints n lines of output. The first line prints a # single 'a', the second line prints 'aa', the third line prints 'aaa', # and so on. # Why didn't we convert n to an integer? # We don't have to! We're assuming the arguments passed in are going to be integers! # When we call the function we will do something like a_ing(5) not a_ing('5') def a_ing( n ): # n is a parameter! output = '' for i in range(0, n): output = output + 'a' print( output ) # also could've done: # for i in range(1,n+1): # output = 'a' * i # print( output )