Functions
A function is a block of code that performs a specific task. The code written in the function is not executed until we call it. The function is separated from the main function and when we need to perform the task, we call the function.
There are two types of functions:
- Built-in functions - already coded in python. We just have to call them. For example, len(), dict(), list(), tuple() and many more.
- User defined functions - defined by users (us). We will study user-defined functions in this section.
In python, functions are declared using def keyword.
If you make a calculator in your main function and you need calculator 10 times. You will have to write code for it 10 times. If you declare a function and write code for calculator there. And then if you need calculator 10 times, then you just need to call calculator function, which will be a single line of code. Furthermore, if error occurs in the code, you just have to check your function.
Difference between Arguments and Parameters
Arguments and parameters are not the same things.
- Parameters: The variables passed in the parenthesis when we define a function are called parameters.
- Arguments: The variables passed in the parenthesis when we call a function are called arguments.
They are used to send data to a function.
A simple explanation
- nameOfStd, rollNoOfStd, marksOfStd are parameters.
- name, rollNo, marks are arguments.
Note: We can add as many arguments as we want. And for now, keep in your mind that the number of arguments and parameters must be same.
Arbitrary Arguments
If we do not know the number of arguments, which will be passed to a function, use a single parameter and add an asterisk sign before its name in the function definition.
A simple explanation
This *data variable acts as a tuple and recieves a collection of variables at the time of function call. The items in the tuple are ordered in the same way as they have been passed to function as arguments.
Keyword Arguments
The keyword arguments are the arguments which are passed to the function along with a key. We can access the variables in the function using key. Therefore, the order of passing arguments does not matter.
Arbitrary Keyword Arguments
If we do not know the number of keyword arguments, which will be passed to a function, use a single parameter and add a double asterisk sign before its name in the function definition.
This **persons variable acts as a dictionary and receives a collection of key value pairs at the time of function call.
Default Parameters
If we call the function without any value, we can set the default value for our parameters. We have studied that if the number of arguments and parameters are not same, error will occur, but if we set the default value of our parameters and do not pass any argument, no error will occur.
Return Values
In Python, functions return some values in the main function. We have to use return statement for this purpose.
Recursion
Recursion refers to a technique where a function calls itself to solve a problem by breaking it down into smaller subproblems of the same type.
The Pass Statement
We have studied about pass statement in if-statements. As the definition of if-statements can not be empty, in the same way function definition can not be empty. If we want to declare a function and do not want to define then we have to write pass statement. We can not leave the body empty.
Why do we need functions?
- Modularity and Abstraction: Functions allow you to break down your code into smaller, manageable chunks, each responsible for a specific task.
- Code Reusability: Once you've written a function to perform a particular task, you can reuse it in multiple places throughout your code.
- Testing: Functions can be tested individually, making it easier to identify and fix issues in isolation.
There are many other advantages of using functions.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP