Python - (def|define) function (procedure)

Card Puncher Data Processing


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)


  • the argument is the calling variable of the function,
  • the parameter is the variable of the function definition.

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 generates a function and assigns it to a name.


Python - Data Type

def addS(x):
    return x + 's'
print type(addS)
print addS
<type 'function'>
<function addS at 0xb0ef95dc>

Splat arguments

Splat arguments are an arbitrary number of arguments.

  • String
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')

  • Number
def biggest_number(*args):
    print max(args)

biggest_number(-10, -5, 5, 10)

built-in functions

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.

Documentation / Reference

Discover More
Card Puncher Data Processing
Language - (Import|Include|Require)

import basically makes names (generally of function) available to the current script. To load modules, language uses different expression: imports includes. require (Node) The import functionality...
Card Puncher Data Processing
Python - (Function|Procedure|definition)

in Python. 3155PEP 3155 Qualified name for classes and functions. A function in Python has a type. See
Card Puncher Data Processing
Python - Name

in Python Names (refer to|are pointer for) objects. Names are introduced by name binding operations such as during the def process. targets that are identifiers if occurring in an assignment,...
Card Puncher Data Processing
Python - Script

in Python are source file that can be run. On Linux, begin your scripts with your interpreter. See or You can find them by executing the whereis commando: sys.argv[0] is the script name...
Card Puncher Data Processing
Python - lambda functions

in Python. See The expression below yields a function object. The unnamed object behaves like a function object defined with lambda generates a function and returns it, while def generates a...
Card Puncher Data Processing
Python Package - Import

import functionality in Python In order to use the names (generally, variable and function definitions) defined in a module, you must either: import the module itself or import the specific definitions,...
Card Puncher Data Processing
Python Package - Module

On a file system, package are the directories and modules are the files within this directories. A module is a file that contains structure definitions including : variables class and functions....

Share this page:
Follow us:
Task Runner