# functions.py # Let's start with somethig that we are all familiar with. # # In Math, we (1) define and (2) apply a function in that order # in 2 steps like this: # # (1) Definition: f(x) = 3x^2 + 2x + 1 # # (2) Application: f(2) = 12 + 4 + 1 = 17 # f(3) = 27 + 6 + 1 = 34 # # In Python, the idea is the same. We only happen to be using # a different language: different syntax, same semantics (at # least for now): # # Definition: # # def f (x): # return 3*x**2 + 2*x + 1 # # Application: # # f(2) # # which would return 17 as its value # # Let's look at it in more detail. # # In Python, a function must be defined using the following syntax: # # def (): # # # You can make up any name you want for the function that you create, # except that you can't use a name that is a Python keyword. For # example, 'def' is a keyward reserved in the language Python, and you # must avoid using it as a name that you create. # The list of parameters specifies what information, if any, you # have to provide in order to use the function. # # There can be one or more of statements inside the function, but # they MUST be indented from the def. In my examples, I will use # the standard indentation of four blank spaces. # # Function definitions are the first of several compound statements # we will see, all of which have the same pattern: # # 1. A header, which begins with a keyword and ends with a colon (:). # # In case you have not noticed it, the right parenthsis, the left # parenthesis, and the colon are all required. Seemingly # insignificant letters like these are part of the language syntax # and Python like any other programming language is very picky # about the exact syntax. # # 2. A body consisting of one or more Python statements, each # indented the same amount from the header. # # In a function definition, the keyword in the header is def, which is # followed by the name of the function and a list of parameters enclosed # in parentheses. The parameter list may be empty, or it may contain # any number of parameters. In either case, the parentheses are required # as is the colon (:). # def f(x): return 3*x**2 + 2*x + 1 print f(1) def g(x, y, z): return x * y + z # Run the programs in this file and see what the comma following the second # double quote does below. It mearly separates two values that the print # statement prints. I will often use this sort of output format which will # make reading the output easier. # print "g(2, 3, 4) = ", g(2, 3, 4) # pow for power and abs for absolute are two of the many functions that # are already defined in the language that we can use. Those language # provided functions are called built-in functions as opposed to the # ones that we programmers create, which are called user-defined functions. # For example, f and g above are user-defined functions. # print "f(pow(abs(-2), 3)) = ", f(pow(abs(-2), 3)) # There are some libraries (packages) that need to be 'imported' into the # language before we can use any function that is defined in the library. # 'math' is one such library that contains many math related functions. # We import a library by the 'import' command like below: # import math # distance computes the distance between two points: p1(x1, y1) and p2(x2, y2) # def distance(x1, y1, x2, y2): dx = x2 - x1 dy = y2 - y1 dsquared = dx**2 + dy**2 result = math.sqrt(dsquared) return result print "distance(0, 0, 3, 4) = ", distance(0, 0, 3, 4) # Note that when we want to use/call/invoke/apply a function defined in a # library (aka package) other than the default package, we must specify the # name of the package followed by a dot (.) followed by the function that we # want to use in the package. Note that we did not have to use any package # qualifier when we used the ones like print and def because they are # included in the base Python language package, i.e., default package. # isEven tells if a given number is even or odd. # % computes modulo # == tests to see if two values are equal or not producing a boolean value, # i.e., True or False # def isEven(x): return ((x % 2) == 0) print "isEven(3) = ", isEven(3) def isDivisible(x,y): return (x % y) == 0 print "isDivisible(30, 3) = ", isDivisible(30, 3) # Exercise 1. # # Define a function that converts a Celcius temperature value to a Fehrenheit # temperature and call it with some values such as 100, 0, -40, etc. # I will provide my answer to this exercise at the end of this file, but try # it yourself before you see mine. # Exercise 2. # # This time let us combine functions and input. Define a function that # reads a number from the user through keyboard input, calls the function # that converts the number as Celcius temperature into Fehenheit temperature, # and returns the computed Fehrenheit temperature. # # After that you can run this program three times: once each with 100, 0, # and -40 as input values. # # I will also provide my answer to this exercise at the end of this file, but # try it yourself before you see mine. # Exercise 3. # # Define a function with three string parameters and returns a string # that is the result of concatenating the three with a space as a separating # character between strings. Now, call the function with three strings as its # actual arguments. # Exercise 4. # # See Section 3.15 of [Downey] if you want to see more exercise problems. # Sample solution to Exercise 1 above. def c2f(c): return c * (9.0/5.0) + 32 # Sample solution to Exercise 2 above. def c2ftest(): n = input("Enter a number: ") n = c2f(n) return n print c2ftest() # use 100 print c2ftest() # use 0 print c2ftest() # use -40 # Note that c2ftest may be rewritten more succinctly this way: # # There are usually more than one way to write a program that produces # the correct result. We as a programmer should try to come up with # a good one. def c2ftest2(): return c2f(input("Enter a number: ")) print c2ftest2() # use 100 print c2ftest2() # use 0 print c2ftest2() # use -40