Python Functions Are Little Machines in Your Program

Python Functions Are Little Machines in Your Program

Functions are like little machines in your program. You put something in the machine, press the start button, it works its magic and spits something out. Python functions are incredibly useful. They allow us to reuse code and simplify our programs. The last thing you want to do is waste your time rewriting the same thing over and over. Remember this mantra: Don’t Repeat Yourself. This is called the DRY principle. Not only is it an important tenet of software engineering, it’s also a good rule for life.

Python Functions Arguments

How Functions Work

Let’s imagine we have a toaster function. It would look like this:

You know you’re dealing with a function when you see a string of characters immediately followed by parentheses. Functions are defined by a name and accept an argument, or multiple arguments. The parentheses are used to ‘catch’ the argument. Why is it called an argument? The history of computing extends back to mathematics and further to astronomy. Latin was then the official language of the sciences and the word argumentum means evidence or proof. So when we pass an argument to a function, we are working with something definite and tangible. Do you want to argue about it?

We pass our toaster argument a piece of bread, like this:

After performing its operations, the toaster function returns our bread, transformed! Using a function is referred to as a function call. It’s like phoning a friend and asking the time. Your friend is the function. You make the call. Your friend returns the time. Let’s write that in pseudocode:

Built-In Python Functions

You’ve been using functions from the get-go.

This is what you’ve encountered so far:

These are a few of Python’s built-in functions.  Built-in functions are processes used so frequently that they are included with Python. For a complete list, visit:

Python Functions

Was our tutorial function successful? Let us know in the comments below. The next tutorial puts the FUN in function.

Python Functions Are Little Machines in Your Program 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