Python Function Arguments Explained With Examples
Table of Contents
- What are Function Arguments in Python?
- Default Arguments
- Keyword Arguments
- Required arguments
- Arbitrary Arguments
What are Function Arguments in Python?
Arguments are the things that are passed to any function or method call, and the arguments are referred to by their parameter names in the function or method code. Python UDFs can take four different sorts of parameters.
Default Arguments
If no argument value is given during the function call, the default argument is used. The assignment operator = must be used to set this default value.
Keyword Arguments
You may utilise keyword arguments in your function call to ensure that you call all of the parameters in the correct sequence. These are used to identify arguments based on their parameter names.
Required arguments
A UDF's required arguments are those that must be present. These parameters must be given in the correct order during the function call.
Arbitrary Arguments
If you don't know how many arguments your function will get, use a * in front of the parameter name in the function specification.
Code:
def myfunction(name):
print(name + "hello")
myfunction(“python")
Output:
Types of Functions in Python
The following examples demonstrate the many types of functions accessible in Python programming.
-
Python Function with No argument & No Return value
In this type of Python function, we won't send any parameters to the function when creating, declaring, or calling it.
When we call this type of Python function, it will not return any value. When there isn't going to be a return value, we may need to use print statements as output. In such a case, we may use Python's built-in functions.
Code:
def Add():
a = 10
b = 20
Sum = a + b
print( Sum)
Add()
30
-
Python Function with no argument & with a Return value
In this type of Python function, we won't send any parameters to the function when creating, declaring, or calling it.
When we call this type of Python function, it will not return any value. When there isn't going to be a return value, we may need to use print statements as output. In such a case, we may use Python's built-in functions.
Code:
def Add():
a = 10
b = 20
Sum = a + b
Return Sum
print(Add())
30
-
Python Function with argument & No Return value
If you look at the above two types of functions, you'll see that no matter how many times you execute them, Python always produces the same result.
Because the variable values (a, b) are fixed, we have no influence over them. We usually deal with dynamic data in real-time, which means we must allow the user to add his own values rather than predefined ones.
Code:
def Add(a,b):
Sum = a + b
print(Sum)
Add(10,20)
30
-
Python Function with argument & Return value
This function allows us to pass the function's parameters when calling it. When we call this type of function in Python, it returns some value. This form of user-defined function, known as a completely dynamic function, gives the end-user complete control.
Code:
def Add(a,b):
Sum = a + b
return Sum
print(Add(10,20))
30
Video : https://youtu.be/DsCazsOPHSc