Python – Writing Functions – Complete Guide

Writing Python code is fun and easy. You can use Procedural programming, Functional programming , Object oriented programming or any combination.  Functions are basic unit in any method you choose.

Functions are objects, defined with the ‘def’ statement followed by argument list. The function body is defined using indentation like other python statements

Arguments are named, defaults may be assigned , return statement is optional and you can return anything. Variables inside functions are local unless the keyword ‘global’ is used

Default Values

You can assign default values to the parameters:

if one parameter is defaulted then all that follow must also be defaulted too so def fn(a=100, b) gives a syntax error

Passing parameters by name

You can ignore the parameter position and pass it by name. This is useful when functions have many parameters with default values:

Enforcing Named Parameters (Python 3)

You can use * to force a user to supply named arguments:


Variadic Functions

Variadic functions have a variable number of parameters. They can be collected into a tuple with a * prefix


Keyword Parameters

Keyword parameters (kwargs) enable the parameters to be passed as a disctionary. The parameters are placed into a dictionary means that the user does not need to get the order correct.  This is useful with a long parameter list for example while connecting to a database (user, password, dbname, …) . A kwargs parameter may only be used at the end of a parameter list.

You can send named parameters or a dictionary. Use ** if caller’s parameters are in a dictionary


Global Variables

The rules of scope in Python are that an undefined variable used in a function must be a global, but if a value is assigned in the function before it is used then it is a local.

For example:

The function f1 declares a local copy of num so it won’t change the global value. To access the global num:


Nested Functions

You can place a function inside another function for simple scope or for closure

In the following example only f1 can use the function f2.

variables in nested functions:

If we define a variables with the same name in a different scopes it may confuses the interpreter for example:

This code will fail with the error: UnboundLocalError: local variable ‘b’ referenced before assignment

To fix this you need to declare b as global or nonlocal





Functions are objects so you can pass a function as a parameter to another function or return a function from another function. This is useful when you write a function that create and return another function dynamically for example, gets a list of points (x,y) and returns an interpolation function.


Each call to getmulby returns a new function



Decorator is a good example of closure. Decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it

Before calling my_function we are actually calling add_stars sending my_function as a parameter. The function add_stars returns the inner function (closure)  and this is what we really invoke.

This is very helpful while writing infrastructures


Lambda Expressions

lambda functions are anonymous functions for simple operations. It is very common where we pass a function to another function. For example the builtin map function to map a list to a new list:

lambda functions cannot contain branches or loops but can contain conditional expression. Usually a simple expression:


Function Documentation

You can add docstring comments to a function. It is used for help function and automated testing.

It is stored in an attribute __doc__. You can set it explicitly


Function Annotations (Python 3)

You can add documentation to the parameters and the return value:


Subscribe to our list









6 thoughts on “Python – Writing Functions – Complete Guide

  1. Please use pep8

  2. […] Python – Writing Functions – Complete Guide […]

  3. VPN tunnel by VPNKI with python

Leave a Reply

Your email address will not be published.