FRUITFUL FUNCTIONS
The
built-in functions we have used, such as abs, pow, and max, 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
But so
far, none of the functions we have written has returned a value. In this
chapter, we are going to write functions that return values, which we will call
fruitful functions, for want of a better name. The first example is area, which
returns the area of a circle with the given radius:
def area(radius):
temp = 3.14159 * radius**2
return temp
We have
seen the return statement before, but in a fruitful function the return
statement includes a return value. This statement means: Return immediately
from this function and use the following expression as a return value. The
expression provided can be arbitrarily complicated, so we could have written
this function more concisely:
def area(radius):
return 3.14159 * radius**2
On the
other hand, temporary variables like temp often make debugging easier.
Sometimes
it is useful to have multiple return statements, one in each branch of a
conditional.
We have
already seen the built-in abs, now we see how to write our own:
def
absolute_value(x):
if x < 0:
return -x
else:
return x
Since
these return statements are in an alternative conditional, only one will be
executed. As soon as one is executed, the function terminates without executing
any subsequent statements. Another way to write the above function is to leave
out the else and just follow the if condition by the second return statement.
def
absolute_value(x):
if x < 0:
return -x
return x
Think
about this version and convince yourself it works the same as the first one.
Code that
appears after a return statement, or any other place the flow of execution can
never reach, is called dead code.
In a
fruitful function, it is a good idea to ensure that every possible path through
the program hits a return statement. The following version of absolute_value
fails to do this:
def
absolute_value(x):
if x < 0:
return -x
elif x > 0:
return x
This
version is not correct because if x happens to be 0, neither condition is true,
and the function ends without hitting a return statement. In this case, the
return value is a special value called None:
>>>print absolute_value(0)
None
None is
the unique value of a type called the NoneType:
>>> type(None)
All
Python functions return None whenever they do not return another value.
Scope of
a variable is the portion of a program where the variable is recognized.
Parameters and variables defined inside a function is not visible from outside.
Hence, they have a local scope.
Lifetime
of a variable is the period throughout which the variable exits in the memory.
The
lifetime of variables inside a function is as long as the function executes.
They are
destroyed once we return from the function. Hence, a function does not remember
the value of a variable from its previous calls.
Eg:
def
my_func():
x = 10
print("Value
inside function:",x)
x = 20
my_func()
print("Value
outside function:",x)
Output:
Value inside function: 10
Value outside function: 20
Local Scope and Local Variables
A local variable is a variable that is
only accessible from within a given function. Such variables are said to have local scope .
A global variable is a variable that is
defined outside of any function definition. Such variables are said to have global scope .
Variable
max is defi ned outside func1 and func2 and therefore “global” to each. Function Composition
As you
should expect by now, you can call one function from within another. This
ability is called composition.
As an
example, we’ll write a function that takes two points, the center of the circle
and a point on the perimeter, and computes the area of the circle.
Assume
that the center point is stored in the variables xc and yc, and the perimeter
point is in xp and yp. The first step is to find the radius of the circle,
which is the distance between the two points. Fortunately, we’ve just written a
function, distance, that does just that, so now all we have to do is use it:
radius = distance(xc, yc, xp, yp)
The
second step is to find the area of a circle with that radius and return it.
Again we will use one of our earlier functions:
result = area(radius)
return
result Wrapping that up in a function, we get:
def area2(xc, yc, xp, yp):
radius = distance(xc, yc, xp, yp)
result = area(radius) return result
We called
this function area2 to distinguish it from the area function defined earlier.
There can only be one function with a given name within a given module. The
temporary variables radius and result are useful for development and debugging,
but once the program is working, we can make it more concise by composing the
function calls:
def area2(xc, yc, xp, yp):
return area(distance(xc, yc, xp, yp))
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.