Functions and Defining Functions
A function is a sequence that performs a computation. To use a function, we call it. We have already been through some functions like the print () command. This is a function and we are calling the function by typing in "print". We have done mathematical functions as well (addition, subtraction, multiplication, and division).
These are just a few examples of functions.
In Python, we can make our own functions, and this is exactly what a module is. A module is a built-in, ready-to-use Python function. When we install a module, we download a ton of data, and all this data is a code; however, when we use these modules in Python, we do not see the code. So, how are modules made?
Modules are made by defining the functions. And to define, we use def() and then, with an indentation, we write our code.
For example, let's write a simple function to print "Hello World".
In this code, we defined a function and named it "first_function", and then used a colon. The colon defines what's under the function we just created ("first_function"). And in Python, there is an indentation of 4 spaces, or 1 TAB. We ask Python to print Hello World!, and now all that's left is to call the function.
In this function, we named it "echo" and passed an argument which is named "string", and we write the code to get the string twice on the output... like an echo, get it? This is a bit different than the previous one.
After the name and indentation, we have the syntax. We passed an argument while defining the function and we called to print the string for the first time, and then print the string again. So now, if we have to call the function, we have to pass an argument inside the parentheses.
It is also crucial to remember that when a variable is created inside a function, that variable is confined solely to that function and can not be used outside of that function.