• No results found

As a reminder, we recommend that you have IDLE open (or another Python editor) and that you try all the code under discussion: this is a good way to cement your learning.

The general form of a function call is as follows:

«

function_name

»

(

«

arguments

»

)

An argument is an expression that appears between the parentheses of a function call. In abs(-9), the argument is -9.

Here, we calculate the difference between a day temperature and a night temperature, as might be seen on a weather report (a warm weather system moved in overnight):

>>> day_temperature = 3

>>> night_temperature = 10

>>> abs(day_temperature - night_temperature)

7

In this call on function abs, the argument is day_temperature - night_temperature. Because day_temperature refers to 3 and night_temperature refers to 10, Python evaluates this expression to -7. This value is then passed to function abs, which then returns, or produces, the value 7.

Here are the rules to executing a function call:

1. Evaluate each argument one at a time, working from left to right.

2. Pass the resulting values into the function.

3. Execute the function. When the function call finishes, it produces a value.

Because function calls produce values, they can be used in expressions:

>>> abs(-7) + abs(3.3)

10.3

We can also use function calls as arguments to other functions:

>>> pow(abs(-2), round(4.3))

16

Python sees the call on pow and starts by evaluating the arguments from left to right. The first argument is a call on function abs, so Python executes it.

abs(-2) produces 2, so that’s the first value for the call on pow. Then Python executes round(4.3), which produces 4.

Now that the arguments to the call on function pow have been evaluated, Python finishes calling pow, sending in 2 and 4 as the argument values. That means that pow(abs(-2), round(4.3)) is equivalent to pow(2, 4), and 24 is 16.

Here is a diagram indicating the order in which the various pieces of this expression are evaluated by Python.

We have underlined each subexpression and given it a number to indicate when Python executes or evaluates that subexpression.

Some of the most useful built-in functions are ones that convert from one type to another. Type names int and float can be used as functions:

>>> int(34.6)

In this example, we see that when a floating-point number is converted to an integer, it is truncated, not rounded.

If you’re not sure what a function does, try calling built-in function help, which shows documentation for any function:

>>> help(abs)

Help on built-in function abs in module builtins:

abs(...)

abs(number) -> number

Return the absolute value of the argument.

The first line states which function is being described and which module it belongs to. Modules are an organizational tool in Python and are discussed in Chapter 6, A Modular Approach to Program Organization, on page 99.

The next part describes how to call the function. The arguments are described within the parentheses—here, number means that you can call abs with either an int or a float. After the -> is the return type, which again is either an int or a float. After that is an English description of what the function does when it is called.

Another built-in function is round, which rounds a floating-point number to the nearest integer:

Functions That Python Provides

33

Function round can be called with one or two arguments. If called with one, as we’ve been doing, it rounds to the nearest integer. If called with two, it rounds to a floating-point number, where the second argument indicates the precision:

>>> round(3.141592653, 2)

3.14

The documentation for round indicates that the second argument is optional by surrounding it with brackets:

>>> help(round)

Help on built-in function round in module builtins:

round(...)

round(number[, ndigits]) -> number

Round a number to a given precision in decimal digits (default 0 digits).

This returns an int when called with one argument, otherwise the same type as the number. ndigits may be negative.

Let’s explore built-in function pow by starting with its help documentation:

>>> help(pow)

Help on built-in function pow in module builtins:

pow(...)

pow(x, y[, z]) -> number

With two arguments, equivalent to x**y. With three arguments, equivalent to (x**y) % z, but may be more efficient (e.g. for longs).

This shows that function pow can be called with either two or three arguments.

The English description mentions that when called with two arguments it is equivalent to x ** y. Let’s try it:

>>> pow(2, 4)

16

This call calculates 24. So far, so good. How about with three arguments?

>>> pow(2, 4, 3)

1

We know that 24 is 16, and evaluation of 16 % 3 produces 1.

3.2 Memory Addresses: How Python Keeps Track of Values

Back in Values, Variables, and Computer Memory, on page 16, you learned that Python keeps track of each value in a separate object and that each object has a memory address. You can discover the actual memory address of an object using built-in function id:

>>> help(id)

Help on built-in function id in module builtins:

id(...)

id(object) -> integer

Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.)

How cool is that? Let’s try it:

>>> id(-9)

The addresses you get will probably be different from what’s listed above since values get stored wherever there happens to be free space. Function objects also have memory addresses:

>>> id(abs)

4297868712

>>> id(round)

4297871160

3.3 Defining Our Own Functions

The in functions are useful but pretty generic. Often there aren’t built-in functions that do what we want, such as calculate mileage or play a game of cribbage. When we want functions to do these sorts of things, we have to write them ourselves.

Because we live in Toronto, Canada, we often deal with our neighbor to the south. The United States typically uses Fahrenheit, so we convert from Fahrenheit to Celsius and back a lot. It sure would be nice to be able to do this:

Defining Our Own Functions

35