Chapter 3 Lesson 2 Functions How can we give a name to a set of commands?
Card Question
Match the concept with the example: String List
"Ferraris" ["my","name","is","Mater"]
Functions We have seen some built-in functions. For example, casting functions like str( ) and int( ) are built-in functions we can use. So is the len function for strings. >>> print str(4) + str(1) str has input of type integer and output of type string >>> print int("4") int has input of type string and output of type integer >>> print len(“abc”) len has input of type string and output of type integer
We can also write our own functions. Let’s write a function to say Hi to George. def sayHi( ): print "Hi George" To run this function, we type >>> sayHi( )
Let’s generalize and write a function that says hi to any name it is given. def sayHi2(anyName): print "Hi " + anyName Now anyName is a parameter . When we call the function sayHi2, we provide an argument, or value for the parameter. >>> sayHi2("Martha") This tells the computer to run the code in the function sayHi2 and replace anyName with the string “Martha”
We can have two parameters or more. def sayHi3(greeting,person): print greeting+person Then we need two arguments when we call it: >>> sayHi3("Good morning","Sunshine") The order of the arguments matters. In this call, the parameter greeting gets the value "Good morning" and the parameter person gets the value "Sunshine" What happens if we call >>> sayHi3("aardvark","zebra")
Suppose we want to write a function that asks the user for a name and then prints a personalized greeting. We need to request a string. def sayHi2U( ): name=requestString("What is your name? ") print "Hi "+name This function does not have a parameter, so when we call it, we do not send it an argument. >>> sayHi2U( )
For each of these functions, a) state the function name and the parameters (if any). b) How would you call each of these functions? c) What happens when you call them? def ave90and98( ): test1 = 90 test2 = 98 sum = test1+test2 ave = sum/2 print ave
def average(test1,test2): sum = test1+test2 ave = sum/2 print ave
def askAndAverage( ): test1 = requestInteger("Please enter score on Test 1:") test2 = requestInteger ("Please enter score on Test 2:") sum = test1+test2 ave = sum/2 print ave
Return statements We can have a function return a value. For example, we have the function average: def average(test1,test2): sum = test1+test2 ave = sum/2 print ave We can change print to return
def average(test1,test2): sum = test1+test2 ave = sum/2 return ave
Now when we call it, we give a name to what returns: >>> mygrade = average(90,98) >>> print mygrade
That way, we can do other things with the result. For example, we can add bonus points: def finalgrade(test1,test2,bonus): mygrade = average(test1,test2) print mygrade + bonus >> finalgrade(90,98,5)
Discuss with your neighbor: 1. In this code: def career(person,job): print 'The '+job+' is '+person >>> career('Sally','attorney') a. identify the following: name of the function parameters the value assigned to person b. What prints as the result of this function call? What prints if instead we call the function with >>> career('attorney','Sally') c. Does the colon go with the definition of the function or with the function call?
2. Matching Parameters
output
Return statement
values of the parameters
Arguments
inputs
3. Consider the directions: Write a function that returns the square of a given number. a. How many parameters should your function have? How do you know that? b. Should your function end with a print statement or a return statement? How do you know that? c. Write the function. d. Write a line of code that calls the function to find the square of 5 and gives a name to the result.