Software Design Spring 2008 For today, you should have: 1) read Chapter 3 of How to Think 2) started on Homework 1 3) chosen and ordered a Python book Today: 1) quiz 2) leftovers from notes01 3) the ultra-secret point of Homework 1 4) variables, values and types 5) defining and calling functions For lab: 1) set up printing in your Linux environment 2) finish Homework 1, bring hardcopy to class 3) don't read Chapter 4 yet! For Thursday: 1) read Chapter 5 of How to Think Quiz 1 ------ Function interfaces ------------------- The interface of a function is everything you need to know in order to use the function. 1) the name of the function 2) what module is it in, sometimes; for example, math.cos 3) what arguments does it take 3a) what are the requirements for the arguments: preconditions 4) what does it return as a result 5) what other effects does the function have 5a) what state does it leave the system in: postconditions 6) what errors might occur One of the signs of a good interface is that you should be able to explain it concisely. Values and types ---------------- Everything is an object. Names refer to objects, so we draw them with arrows. x = 1 y = 1 What does the UML state diagram look like? All values have types. The expression 1 creates a reference to an integer object. 1.0 floating-point 1L arbitrary-precision integer! 'allen' string (double quotes work, too) Variables do not have types. Any name can refer to any type. x = 1 x = 1.0 x = 'allen' You don't have to declare variables, but you do have to avoid using keywords as variable names. Choosing good names is surprisingly important. Expressions ----------- Expressions are made up of operands and operators. The effect of the operator sometimes depends on the types of the operands. 1 + 1 integer addition 2.0 + 2.0 floating point addition 'a' + 'llen' string concatenation 'a' * 3 string multiplication? Gotchas 0) string operators: hoax or scam? 1) what if the operands don't match? 2) beware integer division 3) precedence: learn it, live it, forget it Try it out! ----------- In Linux, create a terminal and type python. Python 2.4.3 (#1, Jun 13 2006, 16:41:18) [GCC 4.0.2 20051125 (Red Hat 4.0.2-8)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> The chevron >>> is a prompt. You can type a python expression and the python interpreter will print the result. >>> 'allen' * 10 'allenallenallenallenallenallenallenallenallenallen' If you are not sure whether something is legal, try it! >>> 'allen' / 10 Traceback (most recent call last): File "", line 1, in ? TypeError: unsupported operand type(s) for /: 'str' and 'int' What does this error message mean to you? function definitions -------------------- function def is an example of a compound statement 1) header ending in : 2) indented body By convention, indentation is 4 spaces. End of body marked by an outdented line. def twice(s): print s, s def fourtimes(s): twice(s) twice(s) A function definition doesn't DO anything except: 1) create a function object 2) create a name that refers to the object function calls -------------- To execute the body of the function, you have to call/invoke it 1) built-in functions len('allen') 2) functions from a module import math print math.sin(math.pi/4) - math.sqrt(2)/2 3) user defined functions fourtimes('allen') arguments are expressions: s = 'downey' fourtimes('allen' + ' ' + s) make some errors ---------------- Try out the following and see what goes wrong: 1) leave off the colon : def fun() x = 1 2) mess up the indentation def fun(): x = 1 y = 1 3) call a function with the wrong number of arguments len('allen', 'downey') 4) call something that is not a function x = 5 x(17) function names are variables ---------------------------- Try this out, and see if you can make sense of it. You might want to draw a state diagram. def is_divisible(x, y): if x%y == 0: print 'yes!' print is_divisible f = is_divisible f(100,10) is_divisible = 'banana' print f print is_divisible functions as parameters (early bird special) ----------------------- def do_twice(f, s): f(s) f(s) def print_twice(s): print s, s do_twice(print_twice, 'allen') See http://www.allenallenvolleyball.com/