Function Arguments in Python

User defined functions in Python are an important method to implement code reusability, modularization and better readability. Arguments make an important part of user defined functions for more flexibility. Function Arguments in Python extends the capability of a function, giving freedom to the programmer. In this article you will read and learn about various ways in which you can define function arguments in python programs. So let’s start.

Default Values for Function Argument in Python

The first method of defining function arguments is to declare them with default values. This is the most important and easy way of doing things. If the caller of a function forgets to pass on  the arguments, the default arguments will be used without failure of a program.

Syntax

def func-name(argument-name1=value1, argument-name2=value2,….):

            function body

There can be a mix of required arguments and default arguments. While calling the function the required arguments must be passed. If values for default arguments are passed while calling the function, they will overwrite the default values.

Note: While declaring a function with mix of required and default arguments, the required arguments must precede the default arguments. This is important since the arguments will be assigned left to right. If you miss any required argument and assign value to a default argument, Python function caller will automatically assign this default argument value to the required argument.

Example

def greetings(Title="Mr.", Name="James", Message="Hello"):
    print(Title,Name,Message)    
greetings()	# No arguments passed while calling the function, all default values will be used
greetings("Mrs.","Bella","Good Evening")# All default values will be overwritten by passed values
greetings("Here","Sire")# only first two default values overwritten as two arguments passed while calling
greetings("Hey")")# only first default value overwritten as one argument is passed while calling

Output

Default function arguments in Python

Keyword Function Arguments in Python

Python functions can be declared in a similar way as the default values arguments and called by specifying the argument name and values pair. This is like considering the name of the argument as a keyword and passing it value while calling as keyword=value pair. This is a helpful technique since you don’t need to follow the sequence of declaration of arguments. You can mix-up the keyword=value pairs while calling the function.

When a keyword value pair is not mentioned in the calling of function and if it is declared with a default value, the function call behaves as function arguments with default value. These rules must be considered for Keyword Function Arguments-

  • All keyword arguments can be declared after all required and positional arguments are declared.
  • The keyword arguments must match the arguments accepted by the function.
  • Each keyword argument can be passed only one value in calling function.
  • The function call must send all the required and positional argument values.

Syntax

def func-name(argument-name[s][=value1], [keyword[s]=value[s]],….):

            function body

Example

def process(instate,memorystate="loaded",outstate="ready"):
    print("The process is in", instate, "state and it is ",memorystate," in memory and now is in",outstate,"state")

process("new") #only required argument is passed while calling function. 
process(instate="new", outstate="suspended")# first and third keyword value pairs passed while function call. So you can see that the jumbling of arguments as keywords is possible
process("new",memorystate="removed",outstate="waiting") #passing all three keyword value pairs in sequence
process(outstate="running", instate="ready", memorystate="allocated")#passing all three keyword value pairs out of sequence

Output

Function arguments as keywords

Undefined Arguments Count

 I find this the most interesting way of passing function arguments. This is also known as passing arbitrary argument list in functions. The multiple argument values are passed as a tuple. If the function needs normal arguments they must be added at the beginning of the argument list

Syntax

def func-name(normal argument list, +args, default argument)

            function body

Example

def myorder(custname, *args, brk=','): # required argument before arbitrary argument. Default argument after arbitrary argument
    print(custname,"has ordered a", brk.join(args),"Pizza")

myorder("James")# function call with required argument
myorder("Bella","Cheese","Tomato")")# function call with required argument and arbitrary argument list
myorder("Lena","Jalapeno","Macroni","Tomato","Olive","Paprika")
myorder("Kamil","Jalapeno","Macroni","Tomato","Olive","Paprika",brk="+")    ")")# function call with required argument and arbitrary argument list and overwritten default keyword argument 

Output

Arbitrary Function Arguments

Be First to Comment

Leave a Reply

Your email address will not be published.