Advertisement



< Prev
Next >



Python - Functions with Default Arguments





In our last article, we have explained the concept of defining and calling functions with arguments. Moving ahead, in this article, we are going to explain a very important concept i.e. functions with default arguments.

In Python, a function can be defined with some arguments which are initialized with a value within the parenthesis( ) of the same function and these arguments are also termed as default arguments of the function. Let us understand some features of functions with default arguments with some examples.




Important points about functions with default arguments






Note :


A point to remember - As the name suggests, we can only define default arguments within a function's parenthesis i.e. when we are defining a function.





When defining a function by mixing non-default arguments with default arguments, the default arguments should be placed last. Let us prove this with an example.

# Python - Function with mixing non-default and default arguments



# Defining a function with a non-default(x) and a default argument(s)
def display(s = 'Hi',x):
    print('x : ', x)
    print('s : ', s)
	

print('Calling the show() function with only one argument')
show('Hello', 20)


print('Calling the show() function with two arguments')
show('Hello', 50)


This program reports a SyntaxError which says non-argument follows default argument. Consequently, when defining a function by mixing non-default arguments with default arguments, default arguments should be placed last or an error is reported.



Please share this article -





< Prev
Next >
< Python functions with arguments
Python recursive function >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement