| How to Program, Part IVSometimes we may want to take a calculation and re-use it several times in a program.
In this case, by defining the function named "ring_up" we've saved ourselves some coding. We can use ring_up() to compute the total of our purchases, along with taxes, as well as an itemized list. Each call to "ring_up" executes the four lines of code present in its definition. Notice the syntax: the values "total", "name", and "price" are passed to the function named ring_up. These variables are called "arguments." The "return" line causes an exit from the funtion and returns the computed value. Variables defined inside a function are not visible outside.
But ariables defined outside the function are visible inside.
It is as if the variables inside a function live in their own private world. Functions can have multiple return points.
In our next program, we want to make use of a function we defined in the "shop.py" program above. We can do this using an "import."
Notice what happened. "import shop.py" read the contents of "shop.py" and then made the function "ring_up()" available to us in our ring.py program. However, in order to use it, we have to use the prefix "shop." in front of each call to "ring_up()". One subtle point here that you should be aware of. Integers and floating point numbers are different:
When integers are used, no decimal values are ever calculated. If you want to see decimal points in the result, you have to use them in one of the values involved in the computation. Thus, the tax calculation we did in "shop.py" would fail if we used an integer for the price.
There are lots of standard functions that you can import.
The above program will just print "2" (the function math.sqrt() is the square root function), as you'd expect.
Here we see that it is possible to import variables (in this case "math.pi") as well as functions.
This prints a random number between 0.0 and 1.0. Try it! This next program can be used to fetch the contents of a web page:
This probably looks a bit strange to you. Some variables have methods attached to them. In this case, the variable url has a method attached to it called readline. I don't want to dwell on this too deeply, it's just a hint of things to come. We have, however, already seen this kind of thing in lesson 3. Variables of type list have an "append" method inside them. One last point... You can return more than one value from a function using something called "tuples." We aren't going to go into too much detail about them here, just to show you this quick example:
The idea is that you can group a set of values together in both return statement (line 2), and when the function is called (line 4). Actually, this also works without the parenthesis, but using them might make it easier for you to read the code.
Here is an example that combines what we know about arrays and functions.in it we will build a new array, one in which the order of elements is reversed relative to the original one.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||