bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

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.

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):
return

Learn Python in 16 hrs from experts

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.

Example:

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

Output:

Welcome to Intellipaat

Wish to Learn Python? Click Here

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.
Example:
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)
return;
#calling a fucntion
printOutput(“Welcome to Intellipaat”)

Output:

Welcome to Intellipaat
Become Python Certified in 16 hrs.
CLICK HERE

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.
Example:

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

Output:

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!

Later, for the best of career growth, check out Python training course to get certified. Also, Intellipaat is providing a free Python interview questions and answers, which will help to excel your career.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

"0 Responses on Python Functions"

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
    top

    Sales Offer

    Sign Up or Login to view the Free Python Functions.