From the course: Introduction to Python: Learn How to Program Today with Python by Pearson

Unlock this course with a free trial

Join today to access over 25,400 courses taught by industry experts.

Write your own functions

Write your own functions

So now, let's look at creating our own functions from scratch. And so you can think of functions as like a recipe or a set of instructions that may take some inputs in and may produce some result or output afterwards. So we can test them out in the console, but it might end up being easier working in a file. Let's do that in a second. So to create a function, you're going to use a new keyword called def. That stands for define. You're defining a new function. Then you give it a name. And maybe I'll call it just a basic one called add. Then you put parentheses. Inside the parentheses are just what inputs you want your function to take. So if you don't want any inputs, you can leave it empty. And then at the end of your line, you add a colon. Now is when we start getting into code blocks, and these code blocks in Python are always indented. So the convention is to use four spaces for indentation, though if your code already has a different convention, like two spaces, just use that. So…

Contents