From the course: Python for Students (2019)

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Creating functions

Creating functions

- [Instructor] As programs get longer, we are going to want to start to keep them organized. We use variables to store data that we wanted to use in different parts of our program. Similarly, we are going to use functions to group together behavior. Think of functions a little bit like menu items at a restaurant. When you order a cheeseburger, you don't need to go back to the chef and tell him step by step how to make it. You can simply ask for a cheeseburger and the chef goes through the necessary steps to make it. Let's get started with an example. Here we are printing the string hello to the screen. Instead, let's move this into a function. To do that, I'm going to type the keyword def followed by say hello, which is the name of our function, followed by parenthesis. We end the function definition by using a colon. We then need to indent everything inside the function. When we want to perform the behavior defined by the function we say that we call it. We do…

Contents