Python functions with def statement

A function is a device that groups a set of statements so they can be run more than once in a program. Python functions are the simplest way to package logic you may wish to use in more than one place and more than one time. Functions also provide a tool for splitting large programs into pieces that have well-defined roles.

The def Statements

The def statement creates a function object and assigns it to a name.

def name(arg1, arg2, ... argN):
	statements

The def has a header line that ends with a colon : and followed by a block of statements (function’s body), usually indented (or a simple statement after the colon).

The def header specifies the name of the function followed by a list with zero or more arguments (or parameters) in parentheses.
The argument names in the header are assigned to the objects passed in parentheses at the point of call.
A def statement can appear anywhere, even nested in other statements.

Function bodies often contain a return statement:

def name(arg1, arg2, ... argN):
	return value

When the return is reached, it ends the function call and sends a result back to the caller. The return statement is optional.

Example:

>>> def mult_numb(a, b):
...     return a * b
... 
>>> mult_numb(5, 3)
15

What we have here is a definition typed interactively that defines a function called mult_numb , which returns the product of its two arguments a and b.

The def statement makes a function but does not call it. To call the function you have to type its name followed by a set of parenthesis which may or may not contain arguments. In the example the function call contains two arguments for obvious reason.

We can do many more things with this function:

>>> a = mult_numb(6, 4)
>>> a
24

If a string and an integer are passed to a and b, the multiplication will repeat sequences. What our function means and does depends on what we pass into it.

>>> mult_numb('Honda', 2)
'HondaHonda'

Example:

>>> def items_in_both(item1, item2):
...     res = []
...     for a in item1:
...             if a in item2:
...                     res.append(a)
...     return res
... 
>>> item1 = 'wall'
>>> item2 = 'walk'
>>> 
>>> items_in_both(item1, item2)
['w', 'a', 'l', 'l']
>>> 
>>> i1 = 'abcd'
>>> i2 = 'amct'
>>>
>>> items_in_both(i1, i2)
['a', 'c']

First we declare a function named items_in_both which takes two arguments.
The algorithm within the body of the function is simple. It says that for every item in the first argument, if that item is also in the second argument, append the item to the result.

Of course everything could be written much simpler using a single list comprehension expression [a for a in i1 if a in i2] but our function does the job.

Note:
The res inside items_in_both is a local variable which is visible only to the code inside the function and exists only while the function runs. All these local variables appear when the function is called and disappear when the function exits.

Leave a Reply