# functions.py # Let's start with somethig that we are all familiar with. # # In Math, we define and apply a function like this: # # Definition: f(x) = 3x^2 + 2x + 1 # # Application: f(2) = 12 + 4 + 1 = 17 # # 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 a little more carefully. # # In Python, a function must be defined using the following syntax: # # def NAME(LIST_OF_PARAMETERS): # STATEMENTS # # You can make up any names you want for the functions 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 new 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 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 them are part of the language syntax # and Python just 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. 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 # function 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 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) # isEven tells 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.8 and Section 5.10 of [DEM] 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: def c2ftest2 (): return c2f(input("Enter a number: ")) print c2ftest2() # use 100 print c2ftest2() # use 0 print c2ftest2() # use -40