What is a function in Python?

No one wants to have to write a sonnet of code for every single time you need a piece of software to do a task for you. That’s why in Python you can use functions to get shit done faster.

In Python, the function is little self-contained program that perform a specific task. You can incorporate a larger program into a Python function and execute (call) them at any time. This will save your time and effort of having to tell the computer what to do every time it does some common task.

You can define a function inside a class, module or another function. The function which is declared inside a class is referred as a method.

Functions in Python are objects. They can be altered like other objects in Python. Therefore, functions are also sometimes referred as first-class citizens. We can characterized function into three segements

Functions that are always available for usage

Functions that are contained within external modules, which must be imported

Functions defined by a programmer with the def keyword.

Using Python functions will help in

Decomposing complex problems into simpler pieces

Reducing duplication of code

Reuse of code

Improving clarity of the code

Information hiding

Before we see how a function is declared and called, let’s take a look at what the Python function is composed of.

Syntax of Python function

def function_name ():

Functions in Python are defined using:

The keyword “def”

Followed by the function’s name – here “guru99”

Parentheses – ( )

And a colon –“:”

For example:

def guru99():
print("learning is fun")
guru99()

The function guru99 will be called, and the output of this function will be “learning is fun.”

When declaring the main function, remember that it is essential that you maintain the indentation (space) after declaring the def function, otherwise it will show an “indent error”.

Passing parameters in Python function

Here are some aspects of parameters with def function_name:

When a function is called, variables are created for receiving the function’s arguments

These variables are referred as parameter variables

Parameters can be mandatory or optional

Parameters are called arguments if the function is called

For example:

def guru99(m,n):
print(m+n)
guru99(20,40)

Here we have declared variable (m,n) as (20, 40). So when you execute the code, it will print out the total as “60”.

When to use function instead of method

Functions are supposed to be used for those operations that were generic for a group of types and which were meant to work even for those objects that did not have methods at all (e.g. tuples)

It is preferable to use functions for collection of various object like (map(), apply() etc.

Implementing max(), min() and len() as built-in functions actually require less code than using them as methods for each type

Use a function for calls that make sense across many different object types – for instance, the built-in functions repr and len works when applied to many kinds of objects

Function arguments

Most functions accept arguments. Arguments are values that are sent to the function. The functions process the values and return some value. Python functions can define their arguments with a keyword. When passing multiple argument with keywords, it is important to consider in which order you are passing the arguments. A non-keyword argument may not follow keyword arguments – we use the * operator to indicate that the function will accept an arbitrary number of arguments.

The important characteristic of Python function to remember is that you can redefine any already defined function.

This Python tutorial series was provided by Guru99 – hands-on tutorials for programming languages.