Functions in Python

Introduction

In this article, we will talk about what we call a reusable piece of code called functions.Functions are central to any programming language. Python makes it very easy to define and use functions.

Python functions are more powerful than their C++ counterparts in the sense that it allows default arguments, keyword arguments and can also be passed as arguments to another function.

Like other articles in Python for Developers series, this article is based on examples with required explanations whenever required.

Minimal Python function

A minimal is defined using a def keyword followed by a function-name and a parenthesis ().

Please give a proper name to your functions. Make sure that it starts with lowercase letter so that you do not confuse with a class in your codes. If there are multiple words, separate them with underscores like sum_of_numbers.

Python Functions

Points to ponder about functions are:

  • Defined using def keyword
  • There is no need to specify return type
  • If a function returns nothing it returns None
  • There is no need to provide types of argument of functions
  • Python functions accept default arguments
  • Arguments can be passed using positional and keyword argument as well

Basic Function Calling

A function can be called using name of the function followed by arguments in parenthesis.

There are three ways to call a function:

  • All positional arguments
  • All keyword arguments (order does not matter)
  • Positional arguments followed by keyword arguments (order is important)

Positional argument must be placed before keyword arguments.

Default Parameter

Parameters of a function which has some default value (i.e. if no value is passed while they are called, the default value is used) must be placed at the end of list of arguments of function definitions.

Return Value

If a function returns a value, the same can be catched in a variable. However, if no return statement is explicitly written by the programmer, it is said to return None.

A function in python can return any object/variable - integer, float, strings, list, list of list, tuple, set, dictionary, list of dictionary, function reference.

Don't worry about what five() returns, we will talk about it later. For now, it is sufficient to know that a function can be defined inside another function and be returned.

Any number of positional and keyword arguments

In Python, it is possible to write any function that can take any number of positional arguments followed by any number of keyword arguments.

The traditional name that catches all the positional argument in function definition is called args for arguments and the variable which catches all keyword argument is named kwargs.

Without going too much into the details of it, just remember that such a function has (*args, **kwargs) as arguments in its definition.

In the example shown below, we will just print all the passed arguments to the function.

Conclusion

In this article, we talked about functions which are fundamental to any programming language. We talked about how functions in Python are powerful and take positional and keyword arguments. We also discussed how we can supply default values to arguments in function definitions.

Author image
New Delhi
I am a passionate programmer based in New Delhi, India. I mostly work with Python. Apart from work, I find myself doing poetry and learning about various languages. I prefer chai over coffee.