Python is a general-purpose, high-level programming language that has many applications in web development, data science, software development, and more. It was initially released in the year 1991. Programming in Python involves simple syntax and it works on different platforms such as Windows, Mac, Linux, etc. In this article, we will discuss the Python functions.
Python Functions
A function is a set of related statements written to perform a specific task when it is called. It can take parameters and return a result. It is efficient to use several functions inside a program to perform specific tasks rather than merely writing a large program that performs the entire tasks. Functions are reusable and make the program easy to understand.
Syntax of Python Functions
A simple python function appears like
The syntax of a Python function can be defined as follows:
For example
Function call
A function can be called in any part of the program or inside another function, by passing appropriate parameters to it.
If you want to print the docstring of the function, you can print it by using,
This function displays my name
Return Statement
The ‘return’ statement is placed at the end of the function that returns either data of a specific type or the evaluated result of an expression. If there is no return statement specified as in the above example function, it returns nothing, or to be more precise, a none object is returned.
Scope of variables and Lifetime
The availability of a variable whether inside or outside of a function is called its scope. A variable declared inside a function has recognition only inside the function and has no scope outside the function. Such variables are called the local variable. A variable declared on the python program (outside a function) is called a global variable and has a scope on the entire code and also inside the functions. You can also make a local variable global by adding the keyword “global” before the variable name.
Lifetime is the period of a variable that has its existence in memory. Variables declared within the function get destroyed when the function completes its execution. However, the global variables persist as long as the entire program is in execution.
We can see an example for local and global variables
Output
If you try executing this function it will show an error message like “name ‘a’ is not defined” as we used the variable outside its scope.
There is also another variable called non-local variables that are used within the nested functions. The non-local variables are declared using the ‘nonlocal’ keyword.
There are two types of functions available in Python. They are:
Types of Function Arguments
The values passed to the function as parameters are called the function arguments. There are two types of arguments. They are
For example,
The function func1 has two arguments, one with the default value and another without any default value. It is mandatory to pass value to the num2 during every function call but passing a value to num1 is optional as it already has a default value and both the function calls will not throw any error here.
Output
Output
Here we don’t know the number of arguments we are going to pass during the function call, so we use a for loop for retrieval of the values of arguments.
Python Recursive Functions
A recursive function is a function which calls itself during execution.
for example,
Sometimes it is hard to write a function only using iterations. There we can use recursive functions which are comparatively easier than iterative functions. However, sometimes for complex operations, recursive logic may be hard to understand.
Anonymous Function (Lambda Functions)
So far we have seen functions declared with a name followed by the def keyword. It is also possible to declare functions without a function name by using the lambda keyword and such functions are called the anonymous functions or the lambda functions. They are also used with some of the python built-in functions such as filter() and map().
The syntax of an anonymous function is
lambda arguments: expression
Arguments can be of any number, but there should be only one expression
Output
In this article, we’ve learned about the functions in python, how to declare them, how to define them, the variables’ scope, their lifetime, types of arguments, and also the anonymous functions.
“We transform your idea into reality, reach out to us to discuss it.
Or wanna join our cool team email us at [email protected] or see careers at Startxlabs.”
Author: Akash Upadhyay