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.
So, without any further delay, let’s get started.
Watch this video about functions 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:
While defining function in Python you need to follow the following set of rules:
Syntax for writing a function:
Learn Python in 16 hrs from experts
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.
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.
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.
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?