Python Functions
Now, we are going to head up to learn a very interesting concept, which is a function in Python. Basically, we have been using some functions in Python, right from the beginning. For example, we have used the print function to print some output or input function for taking the user input, and many more functions are there, which we have been using.
So, now the very first question comes out to be, what is a function?
Python Functions
Well, the function is just a block of code, that does some particular task. There may be some function, that adds two numbers, or there may be some function, that counts the number of elements in the array, and so on. So, the function is a block of code, that does some particular task.
We are soon going to explore a lot about the concept of functions. But first of all, Let’s understand some different types of functions.
If we broadly classify the functions, there can be some functions, that are not written by us, and there can be some functions that are written by us. The functions that are not written by us, and which are already available to use, are called built-in functions. On the other hand, the functions that we are going to write, are called user-defined functions.
We have been using the built-in functions as and when required, like the print function, the len function, the input function, and so on.
But, at times, we might be required to create our own functions. The answer to why we might require to create our own function, lies in some advantages of creating the functions, that we are going to address soon. Here, we are going to explore how we can create our own functions, and we will explore the concept further. Here is an insight into what are we going to cover here –
- How we can create our own function?
- Advantages of writing functions
- Passing arguments to the function
- We can pass multiple arguments to the function
- We can return values from the functions
- Classifying functions – on the basis of arguments and return values.
- A function that does not take any arguments and does not return anything.
- A function that takes arguments but does not return anything.
- A function that does not take arguments but returns some data.
- A function that takes arguments and returns data.
- Different types of arguments
- Positional arguments.
- Default arguments.
- Arbitrary arguments.
- Keyword arguments.
- Arbitrary keyword arguments.
- Lambda functions (Anonymous functions)