Writing Python Functions Part 1: Structure

Writing Python Functions Part 1: Structure

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, fun.py.

Enter the following into your file.

Now run fun.py.


Fun, right?

As you may recall, we can pass arguments to a function. Let’s create a new file, snowclone.py. 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.    https://en.wikipedia.org/wiki/Snowclone

Enter the following in your new file:

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

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.

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:

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.

Writing Python Functions Part 1: Structure was posted by on . Jared likes to make things. He really wants you to watch The Hello World Program so you can learn the skills you need to build an awesome future. His contributions to the show include puppetry, 3D animation, doodling and speaking in a bad British accent. And yes, that is a fox sitting on his face.

Discuss this article in the Comments below, or and it with your friends. Learn more about Python by subscribing to The Hello World Newsletter .

Join the Discussion

Subscribe to our Mailing List

Stay up to date on the latest videos and tutorials with our monthly newsletter

The email entered is invalid