Python is an object-oriented, general-purpose programming language. Python is a high-level programming language that generally emphasizes code readability with the help of proper indentation. The foremost advantage of using Python is that it is a dynamic programming language. Python syntax is considered relatively easy to understand. It is almost similar to human language, and Python codes are generally smaller in size and have less number of lines. Python has some really powerful libraries for machine learning. Pytorch and Tensorflow are the most extensively used libraries for machine learning to date. Python is sometimes also referred to as the language of the Future.
Now that we know a little about what Python is and where it is generally used, let us answer the main question. What are functions in Python?
Basically, the function is a block of code that only runs when it is called using the function call. A function may or may not have a return type. In simpler words, the function might return some value on execution to be used for later purposes. Or the function might simply display something when executed. A function that does not return a value is called a void function. We can also provide the function with additional data that may be needed by the function. This additional data is provided to the function using parameters.
Functions in Python can be created using the def keyword. So the most basic function would look like this,
Notice how I have added a block of space before the print keyword. This block of space is called indentation, and it is used to define the block of code that is present inside the function. For instance, if multiple lines were present inside the function, each line would have the same indentation.
After creating the function, we can call it using the function call. The function call for the above-created function would look like this,
We can also pass parameters in the code for better functionality. For instance, if we had to add two numbers using a function, the code would look something like this,
def printNum(first, second):
Notice how I have used the return keyword to return a value. The previous function did not use the return keyword and was a void function. At the same time, this function returns an integer value.
I hope this answer addresses your queries. I have added links for web pages that I found relevant when I was starting my journey of coding. I hope they help you as much as they have helped me.