9/1/2013 Announcements 15-112 Fundamentals of Programming Read pages 29 – 38 (Do not read the section on Break, Continue, and Pass) Reading Quiz due next class Assignment 1 is due Tuesday Sept 1st 2013 What are we doing today? Calling functions Writing functions Functions with parameters Functions with return values Let’s first finish up last lecture Indexing and Slicing Used to manipulate information in a string name = "Chris Myers" print name[2:4] print name[:4] print name [3:] print name[:] 1 9/1/2013 We have used functions before What does ord function do? What does chr function do? What does math.sin function do? Functions that do something Some functions just perform a task def doSomething(): print(“CMU Rocks!”) How would you use this function doSomething() doSomething() Functions Function is a way of packaging a group of instructions that perform a specific task Functions abstract out the “what” from the “how” When we use a function, we worry about “What” needs to be done and NOT “how” it will be done When we write a function we worry about the “how”. Functions that act on input Some functions perform a tasks on values that you give them printSquare – A function that takes a number and prints its square How will you use this function? printSquare(2) printSquare(3) How will you define this function? def printSquare(x): print x, "**2 =", (x*x) 2 9/1/2013 Function definition def SomeName (Input parameters if any): Function Body Function Body Function Body Using Functions A function has to be defined before it can be used! A complete example – funtest.py def printSquare(x): print x, "**2 =", (x*x) printSquare(2) printSquare(3) Functions - multiple parameters Functions with return values Functions can take several parameters Functions can return values def printSum(x,y): print x, "+", y, "=", x+y def square(x): return x*x printSum(2,3) printSum(3,4) print square(3) print square(4) a = square(3) + square(4) print a 3 9/1/2013 Exercise What we know so far Ask the user for their name, and then print the user’s initials. Create a function called getEggCartons: This function should take as input parameter number of egg and return the number of cartons required to hold that many eggs (given that each egg carton holds one dozen eggs, and you cannot buy fractional egg cartons). Read the number of eggs from the user and use the above function to calculate the number of Cartons user should buy Sequential Execution Conditional Execution All execution of instructions so far has been sequential Step 1 Example: print "Hello to my sequential program“ How to get input from the user How to print messages Three type of values Basic Arithmetic Expressions Variables Functions Step 2 name = raw_input("Enter your name> ") Sometimes we want to selectively execute statement If your name is same as mine, I should say something. name = raw_input("Enter your name> ") print "Oh Wow! Your name is same as mine" Step 3 age = int(raw_input("Enter your age> ")) Step 4 Is there anything wrong with this? print name,", you are ", age , "years old" 4 9/1/2013 Conditional Execution We can fix this by using conditional execution name = raw_input("Enter your name> ") Colon if name == "saquib" : print "Oh Wow! Your name is same as mine" Indented Conditional Execution Conditional execution finishes when you stop indenting name = raw_input("Enter your name> ") if (name == "saquib"): print "Oh Wow! Your name is same as mine" print "I really like our name" print "Welcome to my program", name >>> Enter your name> saquib Oh Wow! Your name is same as mine I really like our name Welcome to my program saquib >>> Conditional Execution number = int( raw_input("Enter a number ")) if number > 0: print "The number is positive" print "Thank you for your number" >>> Enter a number 34 The number is positive Thank you for your number >>> >>> Enter your name> Bob Welcome to my program Bob >>> More on forming conditions Conditional Operators and or not >>> Enter a number -5 Thank you for your number >>> 5 9/1/2013 Combining conditions num1 = int(raw_input()) num2 = int(raw_input()) num3 = int(raw_input()) if num1 > num2 and num1 > num3: print num1 if num2 > num1 and num2 > num3: print num2 if num3 > num1 and num3 > num2: print num3 6