def function are one type of function declaration. See Python - (Function|Procedure|definition).
Functions (called also procedure) are defined using the keyword def that stands for define
# Function definition
def myFunction(parameter1, parameter2):
''' This a docstring who describe what the function does'''
# This is the body where you do something
return aVariable
# Calling the function
myFunction(argument1, argument2)
where:
When the procedure is invoked, the formal argument (the variable) is temporarily bound to the actual argument, and the body of the procedure is executed. At the end, the binding of the actual argument is removed. (The binding was temporary.)
def addS(x):
return x + 's'
print type(addS)
print addS
<type 'function'>
<function addS at 0xb0ef95dc>
Splat arguments are an arbitrary number of arguments.
def favorite_actors(*actor):
"""Prints out your favorite actorS (plural!)"""
print "Your favorite actors are:" , actor
favorite_actors("Michael Palin", "John Cleese", "Graham Chapman")
Your favorite actors are: (u'Michael Palin', u'John Cleese', u'Graham Chapman')
def biggest_number(*args):
print max(args)
biggest_number(-10, -5, 5, 10)
10
Built-in functions are function that doesn't need an import statement.
>>> help(randint)
Help on method randint in module random:
randint(self, a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.