Python Tutorial
What is Python ??
Python Get Started
Python Syntax
Python Variables and Types
Python Data Types
Python Operators
Python Input and Output
Python If ... Else
Python While Loops
Python For Loops
Python Strings
Python Functions
Python Lambda Function
Author Profile Altaf Shaikh
Altaf Shaikh

BE Engg | Python Dev | Content Writer | Founder of TeachMeBro | Blockchain Dev | Django

Python Functions

January 18th 2020

Python Functions
Python Functions

Functions

A piece of source code, separate from the larger program, that performs a specific task.

Reasons to use functions:

  1. Reduce complex tasks into simpler tasks
  2. Eliminate duplicate code
  3. Code reuse
  4. Distribute tasks to multiple programmers
  5. Hide implementation details - abstraction
  6. Improves debugging by improving traceability

Functions are defined in Python using def keyword.

Syntax:

def functionname(parameters):
    """function_docstring"""
    # function_statements
    return [expression]

Return Values

To let a function return a value, use the return statement:

 

The pass statement

function definitions cannot be empty, but if you for some reason have a function definition with no content, put in the pass statement to avoid getting an error.

def func():
    pass

func()

Arguments

Any variable, values, or any data types (list, string, set, dictionary) can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

Arguments are often shortened to args in Python documentations.

Parameters or Arguments?

The terms parameter and argument can be used for the same thing: information that are passed into a function.

From a function's perspective:

  • A parameter is the variable listed inside the parentheses in the function definition.
  • An argument is the value that are sent to the function when it is called.

Number of Arguments

By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.

Example: This function expects 2 arguments, and gets 2 arguments

 

If you try to call the function with 1 or 3 arguments, you will get an error:

Example: This function expects 2 arguments, but gets only 1

 

Arbitrary Arguments, *args

If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.

This is known as Packing Arguments.

This way the function will receive a tuple of arguments, and can access the items accordingly:

Example: If the number of arguments is unknown, add a * before the parameter name

Arbitrary Arguments are often shortened to *args in Python documentations.

Keyword Arguments

You can also send arguments with the key = value syntax.

This way the order of the arguments does not matter.

The phrase Keyword Arguments are often shortened to kwargs in Python documentations.

Arbitrary Keyword Arguments, **kwargs

If you do not know how many keyword arguments that will be passed into your function, add two asterix: ** before the parameter name in the function definition.

This way the function will receive a dictionary of arguments, and can access the items accordingly:

Example: If the number of keyword arguments is unknown, add a double ** before the parameter name

Arbitrary Kword Arguments are often shortened to **kwargs in Python documentations.

Default Parameter Value

The following example shows how to use a default parameter value.

If we call the function without argument, it uses the default value:

Recursion

Python also accepts function recursion, which means a defined function can call itself.

Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.

Example: Factorial of a number

Nested Functions

function which is defined inside another function is known as nested function.