return

Functions, Modules, Local & Global
Variables




a function is way for a programmer to organize the
work (the program).
a book writer, organizes a book into Chapters and
into paragraphs
a programmer, organizes a program into Files and
into functions
one difference (benefit) between functions and
paragraphs is that a function can be “re-used”



The Python interpreter has a number of
functions built into it that are always available
Functions may be used as part of an expression
(like variables & values/constants)
for example, abs(x) is a built-in function that
returns the absolute value of a number
http://docs.python.org/library/functions.html
abs(x)¶
Return the absolute value of a number. The argument may be a plain or long
integer or a floating point number. If the argument is a complex number, its
magnitude is returned
cmp(x, y)¶
Compare the two objects x and y and return an integer according to the
outcome. The return value is negative if x < y, zero if x == y and strictly positive
if x > y.
id(object)¶
Return the “identity” of an object. This is an integer (or long integer) which is
guaranteed to be unique and constant for this object during its lifetime. Two
objects with non-overlapping lifetimes may have the same id() value.
(Implementation note: this is the address of the object.)




Functions may take one or more arguments as input
think of a function as a small machine, that (often)
takes input, process the input and gives back some
kind of result
a function can “give back” a result by “returning the
result”.
This means that a function is evaluated, as an
expression is evaluated, whereby a single value (the
result) is calculated.
Example –
no
arguments
Example –
one
argument
Example –
two
arguments
object()
hex(x)
cmp(x, y)



The return instruction/command is used
within a function to “return a result”
The return instruction (command) is used to
identify the function result
The returned value of a function may be
assigned to a variable or used in a print
More about return, later.


The study of “Function with results” will be left to
Chapter 5
Some function do not “yield a result” (return a result);
likely such a function will simply output information
using a print instruction/command (or similar)


Some “Functions without results “ may produce a
“side-effect”; meaning that it may make changes
outside of itself (i.e. a function may store or change
data in a file).
Some programming languages have a name for
“Functions without results”; procedures



Python has a math module that provide most
of the familiar mathematical functions.
A module is a file that contains a collection or
related functions grouped together.
to use a module you must import the module.
>>> import math



dot notation is a format to identify a function
that is part of module
modules may have function members and/or
data members
the parenthesis after a function name would
distinguish it from a data member

Below; an example of sine (function member of math) &
pi (data member of math)
>>> angle = 1.5
>>> height = math.sin(angle)
>>> print height
>>> print math.pi




Local variables are variables that are created inside
a function
the definition (or creation) of a function is the next
topic of study (but the def instruction/command
will be used to create a function)
all variables created inside the function, including
the parameters, are Local variables
Global variables are created outside of functions
Global variables can be shared my functions
http://www.wellho.net/resources/ex.php4?item=y105/locvar.py
#!/usr/local/bin/python
# Variable scope
first = 1
def one():
"Double a global variable, return it + 3."
global first
first *= 2
result = first+3
return result
print one.__doc__
print one()
print one()
print one()
print "first now has the value",first
print "result has the value",result

Local variables & Global variables have scope

Scope is the “life-time” of a variable


the scope of a Local variable is within the
function (if and when the function executes)
the scope of a Global variable is within the
program (if and when the program executes)