Python function optional argument

Python function optional argument

Posted: Ferens Date: 12.06.2017

Python function keyword and optional arguments - In the last article on Python functions , we have learned about basics of Python functions. The key takeaways from that article are as listed below: Function is a sequence of statements grouped together, intended to be re-used in the program.

Functions are used to avoid repetition of the code in your program. Functions reduces redundancy and improves re-usability.

Python: Function Optional Parameter

Once you define a function, it can be used any number of times in the entire program, or even in other programs we are yet to see 'Modules' , where we can learn more about it. We need the def keyword to create a function. It creates an object of function type and assigns a name to it, at the runtime. The syntax for defining a function is as below: Block of code starts Block of code ends When we need to pass some information to a function, we need to use 'parameters' in the function definition.

While calling the function, we provide information to the function through 'arguments'. A function, after performing some operations, gives control back to the caller. While doing so, a function might return a value or rather 'an object' to the caller using return statement. A function without any return value returns None to the caller.

In this article, we are going to learn more about Function Arguments in more details. To begin with, lets start our discussion be revisiting basics of function arguments.

Function Arguments With function arguments, we can pass some information to a function. In order to store these arguments, function uses names in the function definition, called as Parameters. These parameters are the variables in the local scope of that function, to which values of passed in objects through arguments are assigned.

When we pass the arguments in a function call, the sequence in which objects are passed matters a lot. The objects in the argument are assigned to the parameters from left to right.

Also, the number of arguments being passed must be equal to number of parameters in the function definition. Lets verify this with some examples. In order to verify whether the assignment will be from left to right or otherwise, we print the values of x and y.

In our first function call myFunction 20,30 , x which is the first parameter is assigned with the value 20 which is the first argument and y is assigned with the value In our second function call, we swap the order of the arguments and observe that, those are matched to the parameters from left to right. So, you can't just randomly pass arguments in a function call, the order they are passed in matters a lot. This is the normal way of passing arguments.

python - Optional arguments in a function - Code Review Stack Exchange

With Keyword arguments, we can specify which parameter must be assigned which value, in the function call itself, without bothering about the order, which we see next.

In this case, the arguments can be passed in any order. What would have happened, had we passed only two arguments, instead of three? But what if we want to provide only two arguments or even one, expecting that the function should display None if a value is absent? This leads us to discuss on Defaults or Optional arguments. Optional Arguments With defaults , we can assign a default value to a function parameter.

While making a function call, if the argument is not provided, the parameter takes the default value assigned to it. This not only avoids an exception, but makes the argument optional. When the values are specified, default values will be overwritten and parameters will be assigned with the values provided in the argument. In above example, we created a function welcome that takes two arguments name and country. We assign both these variables with default values 'User' and 'India' respectively, making both of these arguments optional.

If we do not specify any of the argument, it's default value will be printed. If we specify it, the default values are overwritten with the values specified. Please observe the default value 'User' getting overwritten with the value 'Mandar' and the value 'India' with 'America'.

Putting it together Having learned Keyword arguments and defaults , we create a list of items which we need to purchase from market. We consider items 'tomato' and 'onion' as mandatory ones and 'pizza' and 'burger' as optional arguments. We started with normal way of passing arguments to a function, in which order of arguments plays the critical role. We then discussed on keyword arguments, with which we can pass arguments with parameters assigned with values, irrespective of the order they appear in the function definition.

python function optional argument

We then discussed about defaults, which help us create optional arguments. We aren't yet done with arguments though, as we are yet to discuss on variable length arguments to a function.

We see them in the next article.

Python syntax and semantics - Wikipedia

Please share your views and opinions in the comments section below and stay connected. Linux How To's Bash Scripting Python.

Pages Home Python Linux Commands Shell Scripting Tutorials Advertise! Friday, 9 December Python Function: Keyword Arguments, Defaults and Optional Arguments. The key takeaways from that article are as listed below:. Block of code ends. With function arguments, we can pass some information to a function. In above example, we have created a function myFunction which can accept two arguments, which then will be assigned to variables x and y.

In above examples, we have assigned values to the function parameters in the function call itself. It throws in exception, saying that 'you have provided only 2 arguments when I expect 3 from you'. With defaults , we can assign a default value to a function parameter.

Scripting Languages I: iwysuhod.web.fc2.com, PHP, Python, Ruby (Sheet One) - Hyperpolyglot

Having learned Keyword arguments and defaults , we create a list of items which we need to purchase from market. With this we close our discussion on passing arguments to functions.

A simple explanation of Python function arguments, keywords, *args, and **kwargs

Share to Twitter Share to Facebook. Newer Post Older Post Home. Pages Python Linux Commands Shell Scripting Tutorials. Popular Posts How to: Configure Ubuntu as a Router Sed Command in Linux - Append and Insert Lines to a File Sed Command in Linux - Delete Lines from a File Working with 'Arrays' in BASH Scripting FUSER Command in Linux Basic Usage of 'ls' Command in Linux With Examples How To: Powered by Blogger Design by Fabthemes Blogger Template by NewBloggerThemes.

Rating 4,3 stars - 617 reviews
inserted by FC2 system