In the user-defined function blog, we’ve seen a simple example for creating and calling a function
Simple Example
def welcome (name,message):
print("Hello, " + name + message)
welcome('Everyone' , ' how are you!')
welcome('Edusera' , ' how are you!')
Output
Hello, Everyone how are you! Hello, Edusera how are you!
from the above code, we can see that the function takes 2 parameters
and while calling them we’ve given two arguments say Everyone and how are you!
What happen when we pass no argument instead of 2 ?
if we didn’t mention any arguments or mentioned only one argument then it’ll return an error message
Example without arguments
def welcome (name,message):
print("Hello, " + name + message)
welcome()
Output error
Traceback (most recent call last): File "<string>", line 3, in <module> TypeError: welcome() missing 2 required positional arguments: 'name' and 'message'
Explanation
here we aren’t passed any arguments while calling the function so python is saying ERROR and asking to pass 2 arguments ‘name’ and ‘message’
Example with only one argument
def welcome (name,message):
print("Hello, " + name + message)
welcome('Everyone')
Output error
Traceback (most recent call last): File "<string>", line 3, in <module> TypeError: welcome() missing 1 required positional argument: 'message'
Explanation
here we have passed only one argument instead of 2 while calling the function so python is saying ERROR and asking to pass one more argument ‘message’
Conclusion
If we declare a function with n number of parameters we can’t call n-1 or n-2 parameters and so on
we should call all the declared parameters if not it’ll return an error
How to solve this error?
there is a way to avoid getting an error for not passing arguments while calling the function
What are default arguments?
we can define Default arguments which will be taken as arguments if none of the argument is given
this will solve the issue of getting errors while we pass without arguments
How to create default arguments?
we can set default arguments by putting an equal sign after an argument during the creation of a function
In this way, we can provide default values to an argument
Example 1
def welcome (name,message= ' welcome' ):
print("Hello, " + name + message)
welcome('Everyone')
Output
Hello, Everyone welcome
Explanation
here we can see
- we’ve set default arguments for messsage parameter
- so it will become as a default parameter
- if we didnt pass any arguments it will take the default arguments
- if we give any argument default arguments will be replaced by the given arguments
Example 2
def welcome (name= 'no username ',message= ' welcome' ):
print("Hello, " + name + message)
welcome( )
welcome('edusera', ' learn python')
Output
Hello, no username welcome Hello, edusera learn python
Explanation
- we’ve set default arguments for both parameters
- line 3
- if we didnt pass any arguments it will take the default arguments
- if we give any argument then default arguments will be replaced by the given arguments