Python User defined functions.

Necessity for function

Function is basically a block of code that can be reused over and over again. python has lots of built in functions, to name a few, print(), len(), str(), type() and etc... In this article we will learn how to write our own function.

Rules for defining functions in python

  1. Begin with keyword def followed by function name, parentheses ( ) and end with colon :.
  2. If the function takes any input argument, then it should be placed inside the aforementioned parentheses.
  3. Code block should be placed below what is mentioned above and should be indented with respect to beginning of function definition.
  4. A function can optionally have a return statement, and the function ends at the return statement. We can pass back any value to the function caller using return statement from the function. Below is the syntax of a typical function.

Let's write a function that calculates sum of 2 values and returns the value to the caller.

Python course
  1. In the above code we have created a function called add that takes 2 arguments or parameters.
  2. Function is written to add any values of a and b and store it on variable c and we return that value c to the caller.
  3. Then we are calling add function by passing the arguments 5 and 8 from inside a print() function.
  4. add function add the parameters we have passed and return the value which is printed out by our predefined print( ) function.

Pass by value

Python course

We have created a list called name which holds a list of individual character and we are passing reference of that list as an argument to a function testPassByValue( ) which modified the actual content in the list which we tested out by printing out the content of the list.

Different types of function Arguments

  1. Required Arguments
  2. Keywords Argument
  3. Default Arguments
  4. Variable-length arguments

Required Arguments

  1. Arguments should be passed to the functions in correct positional order.
  2. Number of arguments in the function call should match exactly with number of arguments in function definition.
Python course

As you can see above I got an error saying I am missing 1 positional value int when I tried to call the function myWeight( ) with insufficient argument.

As you could see below, error is gone once I passed the argument in proper positional order.

Python Course

Keyword Arguments

  1. Caller of the function identifies the arguments by the parameters names defined during function definition.
  2. We don't have to worry about the position of the argument here.

As we could see above position of the arguments doesn't matter and the function caller can identify the arguments based on the keywords name and age.

Default Arguments

Assumes default value if the value is not provided during the function call.

Variable-length Arguments

When a function needs to process more arguments than specified during function definition, then we are in need of Variable-length Arguments.

**kwargs Arguments(key worded Arguments)

Kwargs allow us to pass n number of arguments which are of key and value type during function call.

Python course

In the above example I have created a function that accepts kwargs as arguments. Our function iterates through the arguments and prints out each and every single key value pairs, and then prints out the entire passed argument during function call as a whole.

I have made function call 2 times and each time with a variable number of arguments and our function manages those arguments and does it job beautifully.


args allow us to pass n number of arguments which are of non-keyowrd type during function call.

python course

In the above example I have created a function that accepts *args as argument, iterate through and calculate summation of all the arguments and prints out the total sum.

I have called the function 2 times and each time with variable number of arguments and our function does it job perfectly.

Return Statement

A python will execute the code under a function when it is called and during execution of a function if python come across a return statement, python will stop executing the code under the function and return if a value is given, else none to the function caller.

python course

In the above example I have created a function called returnSate( ) that has 2 print statements but a return statement in between, as we could see, when I called that function, print statement after the return statement never executed.