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

Part 10, Making Our Own Functions

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.

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

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

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?

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 emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

15 Comments

You're using an old version of python, raw_input() is depreciated (it's now input())

We're using python 2.x in this series, feel free to join us. Although the version we use is slightly old, the information within this series will still pertain to scripting with python.

-Defalt

The differences between python 2 and 3 are syntax, usually putting stuff in () but not always. It is good to know both as both is used.

nice job with this serie Defalt +1 kepp' em coming

Hacked by Mr_Nakup3nda

Love this series! Thank you!

Not all functions return something some functions can be Void

This is true, but when we get down to the bare bones here, functions are built to return things. Once the concept of functions is fully understood, they can be used to organize code with or without returning anything.

-Defalt

This may or may not be off topic, but is it possible to write a function in Python (or if one is already included in the Python API) that can scan an active program (Like task manager (Windows), or Activity Monitor (Mac))?

I'm trying to build a tool to detect malicious/suspicious users.

thanks

Yes. You can do anything with functions, they're just a way to organize blocks of code.

-Defalt

but is there a function within the Python api that lets the python script scan a running program for certain keywords (which are not included with a pre-created list)?

There may be. If there is then I haven't had any experience with it

Hello Defalt,

I would first like to thank you from the core of my hear that you've created this tutorial on Python, That I yearned for.

The thing is that I'm having massive confusion on what "return" is or does. I know it ends the function but its not making sense to me in the second "More advanced function" Which is that if we have put arguments in the parenthesis of NewGreeting.. Why did we use "Defalt and Null-Byte" while printing and also why did you print "NewGreeting" but put "Greeting" as variable. I would highly appreciate any elaboration which would clear my confusion.

I'm a very hard core fan of python and I have high hopes that you're tutorial would be the bridge for me to cross this ocean of confusion and become a python pro...

please reply as soon as possible as i'm making notes of your tutorials so that I can review them, when I can't remember something or what something does.

P.s There is none in my city who teaches Python because everybody is teaching PHP or Javascript Or C++ and Some even told me they never heard of Python as a programming language, so your tutorials are the last hope for me because I felt comfortable while reading them...

Sorry for the long post I wanted to be as descriptive as possible for you to understand the confusion and provide a suitable solution.

The_Unknown.

I actually could not understand your question. Please explain the question again and I shall be happy to help

This function takes the arguments submitted to it, then puts them together and returns a string. Functions don't have to return a string, they can return all sorts of types. For instance the function:

def addNumbers( numberA, numberB):
x=numberA+numberA
return x
Would take two arguments and return x. Here's an example of a usage:
if addNumbers( 7, 3 ) > 3:
print "These are greater than 3"
else
print "These numbers are less than 3"

The function is evaluated, and returns 10 in this case, which is greater than three. Hopefully that helps?

Share Your Thoughts

  • Hot
  • Latest