When a python function is called Inside the function the arguments are assigned to variables called parameters select one?

Photo by Sharon McCutcheon from Pexels

  1. default arguments
  2. keyword arguments
  3. positional arguments
  4. arbitrary positional arguments
  5. arbitrary keyword arguments

The function definition starts with the keyword def. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line and must be indented. — python docs

Formal parameters are mentioned in the function definition. Actual parameters(arguments) are passed during a function call.

We can define a function with a variable number of arguments.

  • Default arguments are values that are provided while defining functions.
  • The assignment operator = is used to assign a default value to the argument.
  • Default arguments become optional during the function calls.
  • If we provide a value to the default arguments during function calls, it overrides the default value.
  • The function can have any number of default arguments
  • Default arguments should follow non-default arguments.

Example:

In the below example, the default value is given to argument band c

This function can be called in 3 ways

  1. Giving only the mandatory argument

2. Giving one of the optional arguments.
3 is assigned to a, 4 is assigned to b.

3. Giving all the arguments

Note: Default values are evaluated only once at the point of the function definition in the defining scope. So, it makes a difference when we pass mutable objects like a list or dictionary as default values.

Functions can also be called using keyword arguments of the form kwarg=value.

During a function call, values passed through arguments need not be in the order of parameters in the function definition. This can be achieved by keyword arguments. But all the keyword arguments should match the parameters in the function definition.

Example:

Calling the function add by giving keyword arguments

  1. All parameters are given as keyword arguments, so no need to maintain the same order.

2. During a function call, only giving mandatory argument as a keyword argument. Optional default arguments are skipped.

During a function call, values passed through arguments should be in the order of parameters in the function definition. This is called positional arguments.

Keyword arguments should follow positional arguments only.

Example:

The above function can be called in two ways:

  1. During the function call, all arguments are given as positional arguments. Values passed through arguments are passed to parameters by their position. 10 is assigned to a, 20 is assigned to b and 30 is assigned to c.

2. Giving a mix of positional and keyword arguments, keyword arguments should always follow positional arguments

default vs positional vs keyword arguments:

Important points to remember:

Photo by Author

1. default arguments should follow non-default arguments

2. keyword arguments should follow positional arguments

3. All the keyword arguments passed must match one of the arguments accepted by the function and their order is not important.

4. No argument should receive a value more than once

5. Default arguments are optional arguments

Example 1: Giving only the mandatory arguments

Example 2: Giving all arguments (optional and mandatory arguments)

Variable-length arguments are also known as arbitrary arguments. If we don’t know the number of arguments needed for the function in advance, we can use arbitrary arguments

Two types of arbitrary arguments

  1. arbitrary positional arguments
  2. arbitrary keyword arguments

4. arbitrary positional arguments:

For arbitrary positional argument, an asterisk (*) is placed before a parameter in function definition which can hold non-keyword variable-length arguments. These arguments will be wrapped up in a tuple. Before the variable number of arguments, zero or more normal arguments may occur.

For arbitrary positional argument, a double asterisk (**) is placed before a parameter in a function which can hold keyword variable-length arguments.

Example:

As per the Python Documentation:

By default, arguments may be passed to a Python function either by position or explicitly by keyword. For readability and performance, it makes sense to restrict the way arguments can be passed so that a developer need only look at the function definition to determine if items are passed by position, by position or keyword, or by keyword.

A function definition may look like:

Photo by author

where / and * are optional. If used, these symbols indicate the kind of parameter by how the arguments may be passed to the function: positional-only, positional-or-keyword, and keyword-only.

  1. Positional or keyword arguments
  2. Positional only parameters
  3. Keyword-only arguments

1. Positional or keyword arguments

If / and * are not present in the function definition, arguments may be passed to a function by position or by keyword

2. Positional only parameters

Positional-only parameters are placed before a / (forward-slash) in the function definition. The / is used to logically separate the positional-only parameters from the rest of the parameters. Parameters following the / may be positional-or-keyword or keyword-only.

If we specify keyword arguments for positional only arguments, it will raise TypeError.

3. Keyword only arguments

To mark parameters as keyword-only, place an * in the arguments list just before the first keyword-only parameter.

If we specify positional arguments for keyword-only arguments it will raise TypeError.

All 3 calling conventions are used in the same function

In the below-given example, the function add has all three arguments

a,b — positional only arguments
c-positional or keyword arguments
d-keyword-only arguments

Important points to remember:

  1. Use positional-only if you want the name of the parameters to not be available to the user. This is useful when parameter names have no real meaning.
  2. Use positional-only if you want to enforce the order of the arguments when the function is called.
  3. Use keyword-only when names have meaning and the function definition is more understandable by being explicit with names.
  4. Use keyword-only when you want to prevent users from relying on the position of the argument being passed.

Everything You Ever Wanted to Know About Python Return Statements

Watch this space for more articles on Python and DataScience. If you like to read more of my tutorials, follow me on Medium, LinkedIn, Twitter.

Thanks for reading!