In the previous section, you've learned how to handle File I/O in Python. You used the open() function to create file objects that you could work with. And that wasn't the first time you've heard about functions in this course. In this section, you'll finally dive deeper into the concept of making your code do something with functions.
The Concept Of Functions
Functions are a fundamental concept in programming that touches on two important aspects:
- Composition: You use functions to break your code into smaller chunks that you can arrange and compose together.
- DRY (Don't Repeat Yourself): You write functions to compartmentalize and generalize a sequence of instructions so that you can use the same code for it in multiple places in your script.
Until now, you've mostly written procedural scripts. Python files that execute instructions from top to bottom. Sometimes, your script would do something, and sometimes, it would produce some output and print it to your command line.
You can think of a function like a script, only smaller. Just like your script file, your function will contain a set of instructions. When you call your function, these instructions will be executed, and the function will return some output. This is similar to running your script, having it execute the instructions, and often giving you back some sort of result.
One reason for building functions is also to generalize tasks and help you to avoid repeating code. Instead, you can write the instructions once and then use them multiple times. Ready to start writing your own functions?
Function Definition Syntax
Like so many things in programming, there's a specific syntax that you'll have to follow in order to write a function. Like so many things in Python, this syntax is quite well-readable and straightforward.
Here's what a basic function definition looks like in Python:
def my_func(parameter):
pass # Replace with code that does something
return # Followed by a value that the function gives back
Such a function definition consists of the following parts:
defkeyword: tells Python that you're defining a function- Function name (e.g.,
my_func): a variable name used to refer to your new function object. The name should be descriptive of what the function does. You'll see examples of that later on. - Parameters: Optionally, a function can require some input, called a parameter. You specify which input is needed in the function definition.
- Function body: your instructions that do something. In this example, you see a
passstatement as a placeholder for the code you'll write here. returnstatement: decides what the output of your function will be. A function can either return some value orNone. If you don't specify a value like in the example above, the function will returnNone.
You'll learn more about these different parts of a function in an upcoming lesson. But before that, you'll revisit some functions you've used already in this course and discuss what they do in practice.
Practice with Functions
- Before turning to the next lesson, take a moment and write down a couple of functions that you've already used throughout this course.
- Can you identify which functions require parameters and what these parameters are?
- How could you find out which of the functions you used returned a value and which ones returned
None?
Additional Resources
- Think Python: Functions
Summary: What is a Python Function
- Functions are a fundamental concept in programming that touches on two important aspects:
- You use functions to break your code into smaller chunks that you can arrange and compose together.
- You write functions to compartmentalize and generalize a sequence of instructions so that you can use the same code for it in multiple places in your script.
Parts of Function Syntax
defkeyword- Function name
- Parameters
- Function body
returnstatement