Functions in Python
Functions are used to group together a certain number of related instructions. These are reusable blocks 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, it might or might not return a value.
Watch this video on ‘Python Functions’:
In this module, we will learn all about functions in Python to get started with them. Following is the list of topics we will cover in this module.
- What Is a Function in Python?
- Defining a Function in Python
- Calling a Function in Python
- Adding a Docstring in Python Functions
- Scope of Variables in Python Functions
So, without any further delay, let’s get started.
Enrol yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!
What Is a Function in Python?
Functions in Python are a set of related statements grouped together to carry out a specific task. Including functions in our program helps in making it much more organized and manageable. Especially, if we are working on a large program, having smaller and modular chunks of code blocks will increase the readability of the code along with providing it reusability.
Basically, there are three types of functions:
- Python Built-in functions (an already created, or predefined, function)
- User-defined function (a function created by users as per the requirements)
- Anonymous function (a function having no name)
Defining a Function in Python
While defining a function in Python, we need to follow the below set of rules:
- The def keyword is used to start the function definition.
- The def keyword is followed by a function-name which is followed by parentheses containing the arguments passed by the user and a colon at the end.
- After adding the colon, the body of the function starts with an indented block in a new line.
- The return statement sends a result object back to the caller. A return statement with no argument is equivalent to return none statement.
Syntax for writing a function in Python:
def (arg1, arg2, … argN): return
Calling a Function In Python
Defining a function is not all we have to do in order to start using it in our program. Defining a function only structures the code blocks and gives the function a name. To execute a function, we have to call it. Only when it is specifically called, a function will execute and give the required output. Now, there are two ways in which we can call a function, after we have defined it. We can either call it from another function or we can call it from the Python prompt.
# defining a function def printOutput( str): #This function will print the passed string print (str) return; #calling a function printOutput(“Welcome to Intellipaat”) Output: Welcome to Intellipaat
Go through this Python Course in London to get a clear understanding of Python!
Adding a Docstring in a Python Functions
The first statement or string in any Python function (optional statement) is called a docstring. It is used to briefly and crisply describe what a function does. ‘Docstring’ is the abbreviation for ‘documentation string’.
Even though including a docstring in our function is optional, it is considered a good practice as it increases the readability of the code and makes it easy to understand. We use triple quotes around the string to write a docstring. A docstring can also extend up to multiple lines.
In the example provided for calling a function, we used a comment to describe what the function was going to do. We will do the same in this example as well. Only that, we will use a docstring here 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 function printOutput(“Welcome to Intellipaat”) Output: Welcome to Intellipaat
Learn more about Python from this Python Training in New York to get ahead in your career!
The scope of a variable is a part of the program where the variable is recognizable.
As we have already discussed about local and global variables in the Python Variables module of this tutorial, we know that the variables defined inside a function only have a local scope. Meaning, the variable defined within a function is only recognizable inside that function.
Lifetime of a variable is the time period till when the variable exists in the memory. 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 with variables in Python Functions:
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 brings us to the end of this module in Python Tutorial. Here we have learnt what function is with the help of a few Python function examples. Now, if you are interested to know why python is the most commonly used language for data science, you can go through this Python for Data Science blog
For getting certified and having best career growth, check out our Python training course. Also, Intellipaat is providing free Python interview questions, which will help you excel in your career.