Menu Close


Python: functions

Python is mostly structured programming language, but it has functional programming capabilities. In Python you can define named blocks of code that can be executed on demand. In other languages these kind of blocks are called subroutines, procedures, functions or methods.

Python Function

Actually Python do not have “functions” nor “methods” or “procedures”. In Python we use keyword “def” to define a light-weight object that can act like a math function or like a procedure. It can be used in expressions to calculate one or more results or it can be call to execute one or more actions. 


0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Test this example live: Fibonacci


The description of a function can be created using triple quotes and string as the first statement after the function declaration. This can be used to find information about function using: help(<function_name>)


Functions can return a result. In the next example I calculate a factorial using a recursive function then I return a result:


Function parameters, sometimes called “formal parameters” in fact local variables that can receive values from function call. Parameters can be optional or mandatory. Optional parameters have an initial value specified in function signature. In this case the arguments of the function are optional and do not have to be mentioned in the function call.


To execute a function we use function name follow by a list of arguments enclosed in round brackets and separated by comma: (arg, arg, …). The parenthesis are mandatory after the function name even if there are no mandatory formal parameters defined.

Arguments are paired-up by position or by name using equal sign. Optional parameters and mandatory parameters can coexist, but usually we declare the mandatory parameters first and optional parameters last. For first arguments we can assign values by position while optional parameters can receive value by names.


Next example is using what you have learned so far to create a kind of average function. But this function is good for 2 up to 5 numbers. It also has a defect when establish the divisor. I let you wander what the defect is. My point is you do not know yet how to create a good average function.

Homework: Copy the example above (use double click) and then make your better version using variable argument “varargs”, that you will learn later in this article. Post your snippet on Discord and brag about it on the forum. I will be very impressed and give you kudos.

Variable arguments

A procedure can receive a list of arguments into one single parameter. This feature is sometimes called: “varargs”. For declaring a parameter for varargs we use symbol “*” that represent “the rest” of values.


Test this snippet on-line: Varargs

Note: In the example above I have used new syntax in Python that enable you to define types for parameters, variables and function results. This may surprise you since you know, Python is a dynamic language. It is a good practice to use this style of programming.


A namespace is a composite word from “name” and “space”. It represents a block of code that hold several identifiers. A namespace is defined by a “scope”. This is a region of a program used to define: variables, constants and functions. 


Test this example live: Namespaces

Output of the program:

Name Space Details

The namespaces are nested. The outermost scope is called “global” scope and it create a “global” namespace. Functions can be nested. Inside every function there is a local namespace.


Using “=” will create a new variable in the local scope. If a variable exists already defined in the global scope or in the parent scope it is shadowed. We create a new variable in the local scope that hide the outer scope variable.

To avoid shadowing we have to declare variables using “global” or “nonlocal” keywords. This is necessary for every single nested function that uses other variables than the local variables.

Function attributes

In Python a function is an object. Any object can have attributes that can be created using a dot operator. Function attributes are attached to the function as static variables. This is another alternative to global variables and it can be used to create encapsulated functions that behave like objects.


In the next example the function test_attr() is creating an attribute called counter the first time is called. Then the attribute is used to memorize the current counter value. Next time is able to return the value and increment it for next call.

Test this example live: Attributes

Test output:

Read next: Classes