Passing without using keyword arguments

Some main point to be taken care while passing without using keyword arguments is :

  • The order of parameters should be maintained i.e. the order in which parameters are defined in function should be maintained while calling the function.
  • The values for the non-optional parameters should be passed otherwise it will throw an error.
  • The value of the default arguments can be either passed or ignored.

Below are some codes which explain this concept.

Example 1:

Python3




# Here b is predefined and hence is optional.
def func(a, b=1098):
    return a+b
 
 
print(func(2, 2))
 
# this 1 is represented as 'a' in the function and
# function uses the default value of b
print(func(1))


Output:

4
1099

Example 2: we can also pass strings.

Python3




# Here string2 is the default string used
def fun2(string1, string2="Geeks"):
    print(string1 + string2)
 
 
# calling the function using default value
fun2('GeeksFor')
 
# calling without default value.
fun2('GeeksFor', "Geeks")


Output:

w3wiki
w3wiki

How to pass optional parameters to a function in Python?

In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified.

In this way, the user can call the function by either passing those optional parameters or just passing the required parameters. 

There are two main ways to pass optional parameters in python 

  • Without using keyword arguments.
  • By using keyword arguments.

Similar Reads

Passing without using keyword arguments

Some main point to be taken care while passing without using keyword arguments is :...

Passing with keyword arguments

...