What are Functions in Python?

In Python functions are groups of related statements that can be called together, typically perform a specific task, and which may or may not take a set of parameters or return a value.

Functions can be defined in one place and called or invoked in another place. This helps to make code more modular and easier to understand. It also means that the same function can be called multiple times or in multiple locations.

This help to ensure that although a piece of functionality is used in multiple places; it is only defined once and only needs to be maintained and tested in one location.

How Functions work in Python?

functions in python

Each time the call is made to function_name() the program flow jumps to the body of the function and executes the statements there. Once the function finishes it returns to the point at which the function was called.

Types of Functions

Technically speaking there are two types of functions in Python;

  1. Built-in functions
  2. User-defined functions

Built-in functions are those provided by the language and we have seen several of these already. For example, both print() and input() are built-in functions.

User-defined functions are those written by developers, that is us.

Defining Functions

The basic syntax of function is as below,

def function_name(parameter list):
    """docstring"""
    statement
    statement(s)
  1. All functions are defined using a keyword def which indicates the start of the function. A keyword is a part of the syntax of the Python
    language and cannot be redefined and is not a function.
  2. Name followed by def is called function name 
  3. Naming convention of function name are all lower case with the different elements of the function name separated by an ‘_’
  4. Function can (optionally) have a list of parameters which allow data to be passed into the function. These are optional as not all functions need to be supplied with parameters.
  5. A colon is used to mark the end of the function header and the start of the function body. The function header defines the signature of the function (what its called and the parameters it takes). The function body defines what the function does.
  6. We typically use the triple double quote string format as docstring as this allows the documentation string to go over multiple lines if required.
  7. It is common to use 4 spaces (not a tab) to determine how much to indent the body of a function by.

Example for function

Let have an example of a simple function as below,

def print_msg(message):
    print(message)


print_msg('Hello World')
print_msg('Hello Python')
print_msg('Hello TalksInfo')
print_msg('Hello Users of TalksInfo')

Above code snippet will give us output as below,

Hello World
Hello Python
Hello TalksInfo
Hello Users of TalksInfo

Returning Values from Function

Like other programming language, python also return values in function. Whenever a return statement is encountered within a function then that function will terminate and return any values following the return keyword. Simple example of function with return statement,

def print_msg(message):
    return message.capitalize();


print('Hello '+print_msg('world'))
print('Hello '+print_msg('talksinfo'))
print('Hello '+print_msg('python'))
print('Hello '+print_msg('talksinfo users'))

Above code snippet will give us output as below,

Hello World
Hello Talksinfo
Hello Python
Hello Talksinfo users

Conclusion

In this article, we saw about what is functions in python, how functions work and how it should be declared and used.

Reference

https://docs.python.org/3/library/functions.html – please refer link of Python docs site for more information.