Welcome back! In the last iteration of how to train your python, we covered error detection and handling. Today we'll be diverging from this and discussing functions. More specifically, we'll be creating our own functions. First we'll need to understand exactly what a function is, then we'll get on to making our own! So, let's get started!
A basic definition of a function is a set of instructions that execute upon calling the functions name. These instructions return something. What this means is that once these instructions are complete, it hands the result back to python.
Another important thing to know about functions is the difference between local and global variables. For instance, if we assign the value "Null-Byte" to a variable, we can access it from anywhere in the script, this is a global variable. But if we create a variable inside a function, it is a local variable and can't be accessed outside the function. An important side note is that functions can take arguments just like methods, we'll be taking a closer look at this shortly.
Now that we have a better understanding of what a function is, let's get started on making our own!
When we're making a new function, we need to define it. This means that we need to specify it's name and what arguments it will take. We'll be defining two functions today. Our first will be a demonstration of the concept, meaning that it won't do anything special but it will give us an idea of the layout of a function. Our second function will be a little more advanced, and will solidify what we've covered. So let's get on with the first function!
Now, our first function will be a greeting. It will take it's own input using raw_input() and will give us results directly through printing. Now let's start by defining our function which is done by entering def followed by a space, then the name of our function, then it's arguments in parenthesis...
Alright, here we've defined our function greetings and our parenthesis are empty, meaning we don't take any arguments. Now, let's build the code inside the function. We'll be asking for a name and place, then greeting the user. Let's build this greeting code now...
As we can see in the above example, everything within the function is entered one tab into the function. This designates this code as part of the function. If we enter any of this code without the tab, it won't be a part of the function and will return an error, so be careful! Now that we've built our first function, let's call it...
There we go! Our function works! Now that we have a basic idea of functions, let's get a little more advanced.
Our second function will be a little more advanced than our first. It will take arguments instead of taking it's own input. It will also return our result instead of printing it. So, let's start by defining our function and naming the arguments it will take...
We could think of arguments as variable names. We can use then inside the function and when we finally call the function, the value we give as that argument will be used as the value for that variable.
Now that we've defined our second function, let's build the code inside of it...
Now, this function looks much smaller, but it does the exact same job as the last one. Let's print the result of calling our new greetings function with the arguments we assigned to it....
Let's run through what happened. We gave our function the arguments of our name and where we're from. It then generated a greeting sentence by plugging those argument values into their respective places, and returned this sentence. We then used print and called our function (while handing it the necessary arguments) and it printed the value that our function returned! Let's wrap this whole thing up, shall we?
Today, we covered how to build our own functions! These can be used to make code much, much more organized. Functions are one of the many things that will stick with us throughout our entire scripting career, so we better get really good at them!
Sorry the period of silence. I'm recently having difficulty finding time between work, college, and this project. But don't worry, we will become great script writers, no matter how long it takes! I'll see you for your python's next training session!
Thank you for reading!
Want to start making money as a white hat hacker? Jump-start your hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from cybersecurity professionals.
Other worthwhile deals to check out: