Defining functions

We can define a function using the def keyword, and you can supply default values for the parameters if desired.

#define a function, giving the parameter a default value of 3 in case
#    the caller does not pass anything
def myfunction(n=3)
	"""Print and return the value of parameter n."""
	print "the value of n is: ", n, "\n"
	return n

# call myfunction, passing value 4
myfunction(4)
# call myfunction, but using the default parameter
myfunction()
When calling the function you can also specify which values go with which formal parameters, rather than relying on the default ordering. For example:
def foo(a, b):
	print a,b

# call the function, specifying how the values and parameters match up
#   by keyword, rather than positioning
foo(b=1, a=2)

The default return value from a function is the special value None.

(Note: the blank line after myfunction is significant.)

(Also note the syntax for the first string in the function: this is a documentation string, and is a useful way to build help documentation into your code. If someone was to call help(myfunction) they'd be shown this string.)