Unit 5: Functions

The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these functions generates a value, which we usually assign to a variable or use as part of an expression.

>>> biggest = max(3, 7, 2, 5)
>>> x = abs(3 - 11) + 10

The first example is area function, which returns the area of a circle with the given radius:

>>> def area(radius):
>>>     b = 3.14159 * radius**2
>>>     return b

The return statement includes a return value. This statement means: evaluate the return expression, and then return it immediately as the result of this function. The expression provided can be arbitrarily complicated, so we could have written this function like this:

>>> def area(radius):
>>>     return 3.14159 * radius * radius
>>> def is_divisible(x, y):
>>>     if x % y == 0:
>>>         return True
>>>     else:
>>>         return False

It is common to give Boolean functions names that sound like yes/no questions. is_divisible returns either True or False to indicate whether the x is or is not divisible by y.