Functions in Python

Functions are used to group together a certain number of related instructions. These are reusable block of codes written to carry out a specific task. A function might or might not require inputs. Functions are only executed when they are specifically called. Depending on the task a function is supposed to carry out, a function might or might not return a value.
In this module, we will learn all about functions in Python to get started with it. Following is the list of all the topics that we will cover in this module, in case you want to jump to a specific one.
Python Functions

So, without any further delay, let’s get started.
Watch this video about functions in Python.

Python functions

What is a Function in Python?

Functions in Python is a set of related statements that are grouped together to carry out a specific task. Including functions in our program helps in making the program much more organized and manageable. Specially if you are working on a large program, having smaller and modular chunks of code blocks will increase the readability of the code along with providing re-usability of code.
There are basically following three types of functions:

  • Built-in function (Already created i.e. predefined)
  • User defined function (Created by users according to the requirements)
  • Anonymous function (Functions having no name)

Define a function

While defining function in Python you need to follow the following set of rules:

  • def keyword is used to start the Function Definition
  • def keyword is followed by function-name which is followed by parenthesis which contains the arguments passed by the user and use the colon at the end
  • After adding the colon, start the body of the function with an indented block in the new line.
  • The return statement sends a result object back to the caller. A return statement with no argument is equivalent to return none.

Syntax for writing a function:

def (arg1, arg2,… argN):

Call a function

Defining a function is not all you have to do in order to start using the functions in your program. Defining a function only structures the code blocks and gives the function a name. To execute a function, you have to call it. Only when specifically called, a function will execute and give the required output. Now there are two ways you can call a function after you have defined it. You can either call it from another function or you can call from the Python prompt.

# defining a function
def printOutput( str):
#This function will print the passed string
print (str)
#calling a fucntion
printOutput(“Welcome to Intellipaat”)


Welcome to Intellipaat

Adding Docstring in function

The first statement or string in any function (optional statement) is called docstring. It is used to briefly and crisply describe what a function does. Docstring is short for documentation string.
Even though, including docstring in your function is optional, but it is considered good practice as it increases the readability od the code and makes it is easy to understand. We use triple quotes around the string to write a docstring. Docstring can also extend up to multiple lines.
In the example for calling a function, we have used a comment to describe what the function was going to do, we will do the same in this example, only this time we will use docstring to describe what the function will do.

# defining a function
def printOutput( str):
“’This function will print the passed string’”
print (str)
#calling a fucntion
printOutput(“Welcome to Intellipaat”)


Welcome to Intellipaat

Scope of variable

A scope of a variable is referred to the part of the program where the variable is recognizable
As we have already discussed about local and global variables in the Python variable module of this Python tutorial, we know that the variables defined inside a function only have a local scope. Meaning, that the variable defined in a function is only recognizable inside the function.
Lifetime of a variable is referred to the time period until the variable exists in the memory. The variables defined inside the function only exist as long as the function is being executed. So, the lifetime of a variable defined inside a function ends when we return from the function or when the control comes out of the function.

def func():
x = 5
print(“value of x inside the function”, x)
#calling the function
x = 10
print(“value of x outside the function”, x)


value of x inside the function 5
value of x outside the function 10

This bring us to the end of this module, next module highlights the lambda function in Python, see you there!

Leave a Reply

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

Solve : *
3 + 13 =