Skip to content
Home » Programming » Functions in Python

Functions in Python

Functions in Python

In this post, we will investigate about the Functions in Python. Functions are groups of related statements that can be called together, that 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. This helps to make code more modular and easier to understand. Therefore, the same function can be called multiple times or in multiple locations.

How Functions Work?

When a function is called; the flow of control of a program jumps from where the function was called to the point where the function was defined. The body of the function is then executed before control returns back to where it was called from.

As part of this process, all the values that were in place when the function was called, are stored away (on something called the stack). So that if the function defines its own versions, they do not overwrite each other.

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 is finished, it returns to the point at which the function was invoked.

Types of Functions in Python

Technically speaking there are two types of functions in Python; built-in functions and user-defined functions.

Firstly, Built-in functions are those provided by the language. For examples, both print() and input() are built-in functions. We did not need to define them ourselves as they are provided by Python.

In contrast, User-defined functions are those written by developers. In many cases, most of the programs that you will write will include user-defined functions.

Also Read : Recursion in Python

Defining Functions in Python

The basic syntax of a function is:

def function_name(parameter_list):
       """docstring"""
       statement
       statement(s)

This illustrates several things:

  1. All named functions are defined using the keyword def; this indicates the start of a function definition.
  2. A function can have a name which uniquely identifies it.
  3. The naming conventions also applies to functions; they are all lower case and name separated by ‘_‘.
  4. A function can (optionally) have a list of parameters which allow data to pass into the function.
  5. A colon denotes the end of the function header and start of the function body.
  6. An optional documentation string (i.e. docstring) can be provided to describe about the function work.
  7. It is common to use 4 spaces (not a tab) to determine how much to indent the body of a function by.

An Example Function in Python

The following is one of the simplest functions we can write; as it takes no parameters and has only a single statement that prints our the message ‘Hello World!‘:

def print_msg():
       print('Hello World!')

print_msg()

As a result, the output is

Hello World!

Note to include the round brackets () when you call the function. This is because if you just use the function’s name; then you are referring to memory location not invoking it.

We could modify the function to make it a little more general and reusable by providing a parameter. This parameter is supplies the message to print. For example,

def print_my_msg (msg):
       print(msg)

print_my_msg ('Hello World')
print_my_msg ('Good Day')
print_my_msg ('Welcome')
print_my_msg ('DroxElement')

As a result, the output is

Hello World
Good Day
Welcome
DroxElement

Leave a Reply

Your email address will not be published. Required fields are marked *