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 re-usability.
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):
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
#This function will print the passed string
#calling a function
printOutput(“Welcome to BeingDatum”)
Output:Welcome to BeingDatum
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.
Scope of Variables in Python Functions:
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: