The Hello World Program
The Hello World Program

Writing Python Functions Part 1: Structure

  • Python
  • Lesson 15 of 24

Python’s built-in functions are incredibly useful. Eventually you will find yourself repeating yourself. That’s bad. That’s when programming gets boring. Fortunately, you can write your own functions, which is why the first three letters of function are ‘F-U-N’. If you’re just joining us, you might want to start with our previous tutorial, Python Functions are Little Machines in Your Program.

Writing Python Functions

Now that we’re writing functions, you will want to use IDLE to create new files. It becomes tedious very quickly to enter and reenter functions in the shell prompt. Open IDLE and create a new file,

Enter the following into your file.

def fun():
    print(“Something that provides mirth or amusement”)


Now run

Something that provides mirth or amusement.

Fun, right?

As you may recall, we can pass arguments to a function. Let’s create a new file, A snowclone is like a one sentence Mad Lib. The classic example is: X is the new black, where X is whatever is cool at the moment, like taupe or mauve.

Enter the following in your new file:

def snowclone(x):
    print(x,is the new black.)


We can also pass multiple arguments to a function. Modify so that it looks like this:

def snowclone(x, y):
    print(x,is the new “, y,.)

snowclone(“Python”, “Java”)

Then run it.

Function Structure

Let’s take a look at the structure of a function by breaking down snowclone(). Functions are composed of a header and a body.

def snowclone(x, y): #header
    print(x,is the new “, y,.) #body

The Function Header

We define a function with ‘def’, which is obviously short for define. Then we give the function a name. Since we are writing the function we can name it whatever we want, but it’s best practice to name it something that reflects what it does. In our case, we named it snowclone(). We could easily name it flapdoodle(), but then nobody but us would know what it did without some digging. Lastly, we immediately follow our function name with parentheses, our argument variables, and then a colon.

The Function Body

We indent the next line to specify that everything that follows is a part of this function. Functions can be as long and elaborate as we want. They can even contain functions within themselves. Try this:

def snowclone(x, y):
    print(x, " is the new", y, ".")
    def fun():
        print("Functions are F-U-N!")

snowclone("Fun", "function")

I hope you had fun writing your first functions. Here’s some extra credit: Write a function that prints a limerick with this template:

There once was an X from place B, Who satisfied predicate P, Then X did thing A, In a specified way, Resulting in circumstance C.

A rolling stone gathers momentum. Get up to speed in our next tutorial on Function Scope & Flow.

Hands-on computer science, programming, and web development. Brought to you by Dototot. The contents of this website are licensed under a CC BY-NC-SA 4.0 License.