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
- Begin with keyword def followed by function name, parentheses ( ) and end with colon :.
- If the function takes any input argument, then it should be placed inside the aforementioned parentheses.
- Code block should be placed below what is mentioned above and should be indented with respect to beginning of function definition.
- 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.
- In the above code we have created a function called add that takes 2 arguments or parameters.
- 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.
- Then we are calling add function by passing the arguments 5 and 8 from inside a print() function.
- add function add the parameters we have passed and return the value which is printed out by our predefined print( ) function.
Pass by value
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
- Required Arguments
- Keywords Argument
- Default Arguments
- Variable-length arguments
- Arguments should be passed to the functions in correct positional order.
- Number of arguments in the function call should match exactly with number of arguments in function definition.
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.
- Caller of the function identifies the arguments by the parameters names defined during function definition.
- 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.
Assumes default value if the value is not provided during the function call.
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.
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.
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.
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.
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.