How to Train Your Python: Part 10, Making Our Own Functions

Nov 16, 2015 06:07 PM
635831819349231309.jpg

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!

What Is a Function?

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.

635832649951737000.jpg

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!

Defining Our Function

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!

Our 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...

635832650196912665.jpg

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...

635832650443650934.jpg

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...

635832650568355079.jpg

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

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...

635832650779465536.jpg

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...

635832650892912016.jpg

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....

635832651070269793.jpg

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?

Wrapping It Up

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!

Feedback!

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!

-Defalt

Just updated your iPhone? You'll find new Apple Intelligence capabilities, sudoku puzzles, Camera Control enhancements, volume control limits, layered Voice Memo recordings, and other useful features. Find out what's new and changed on your iPhone with the iOS 18.2 update.

Comments

No Comments Exist

Be the first, drop a comment!