Python Functions is a block of statements that return the specific task.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again. Some Benefits of Using Functions
- Increase Code Readability
- Increase Code Reusability
Python Function Declaration
The syntax to declare a function is:
Types of Functions in Python
There are mainly two types of functions in Python.
- Built-in library function: These are Standard functions in Python that are available to use.
- User-defined function: We can create our own functions based on our requirements.
Creating a function in Python
We can create a Python function using the def keyword.
Calling a Python Function
After creating a function we can call it by using the name of the function followed by parenthesis containing parameters of that particular function.
Welcome to GFG
Python Function with parameters
If you have experience in C/C++ or Java then you must be thinking about the return type of the function and data type of arguments. That is possible in Python as well (specifically for Python 3.5 and above).
Defining and calling a function with parameters
def function_name(parameter: data_type) -> return_type: """Docstring""" # body of the function return expression
The following example uses arguments and parameters that you will learn later in this article so you can come back to it again if not understood.
The addition of 5 and 15 results 20.
Note: The following examples are defined using syntax 1, try to convert them in syntax 2 for practice.
Python Function Arguments
Arguments are the values passed inside the parenthesis of the function. A function can have any number of arguments separated by a comma.
In this example, we will create a simple function to check whether the number passed as an argument to the function is even or odd.
Types of Python Function Arguments
Python supports various types of arguments that can be passed at the time of the function call. In Python, we have the following 4 types of function arguments.
- Default argument
- Keyword arguments (named arguments)
- Positional arguments
- Arbitrary arguments (variable-length arguments *args and **kwargs)
Let’s discuss each type in detail.
A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument. The following example illustrates Default arguments.
x: 10 y: 50
Like C++ default arguments, any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.
The idea is to allow the caller to specify the argument name with values so that the caller does not need to remember the order of parameters.
Geeks Practice Geeks Practice
We used the Position argument during the function call so that the first argument (or value) is assigned to name and the second argument (or value) is assigned to age. By changing the position, or if you forget the order of the positions, the values can be used in the wrong places, as shown in the Case-2 example below, where 27 is assigned to the name and Suraj is assigned to the age.
Case-1: Hi, I am Suraj My age is 27 Case-2: Hi, I am 27 My age is Suraj
Arbitrary Keyword Arguments
In Python Arbitrary Keyword Arguments, *args, and **kwargs can pass a variable number of arguments to a function using special symbols. There are two special symbols:
- *args in Python (Non-Keyword Arguments)
- **kwargs in Python (Keyword Arguments)
Example 1: Variable length non-keywords argument
Hello Welcome to GeeksforGeeks
Example 2: Variable length keyword arguments
first == Geeks mid == for last == Geeks
The first string after the function is called the Document string or Docstring in short. This is used to describe the functionality of the function. The use of docstring in functions is optional but it is considered a good practice.
The below syntax can be used to print out the docstring of a function:
Example: Adding Docstring to the function
Function to check if the number is even or odd
Python Function within Functions
A function that is defined inside another function is known as the inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function.
I love GeeksforGeeks
Anonymous functions in Python Function
In Python, an anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. Please see this for details.
Return statement in Python function
The function return statement is used to exit from a function and go back to the function caller and return the specified value or data item to the caller. The syntax for the return statement is:
The return statement can consist of a variable, an expression, or a constant which is returned at the end of the function execution. If none of the above is present with the return statement a None object is returned.
Example: Python Function Return Statement
Pass by Reference or pass by value
One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is the same as reference passing in Java.
[20, 11, 12, 13, 14, 15]
When we pass a reference and change the received reference to something else, the connection between the passed and received parameter is broken. For example, consider the below program as follows:
[10, 11, 12, 13, 14, 15]
Another example demonstrates that the reference link is broken if we assign a new value (inside the function).
Exercise: Try to guess the output of the following code.
Quick Links :
- Quiz on Python Functions
- Difference between Method and Function in Python
- First Class functions in Python
- Recent articles on Python Functions.
Please Login to comment...