Creating Functions
For more information on concepts covered in this lesson, you can check out How to Write Beautiful Code With PEP 8.
00:00 Creating Functions in Python for Reusing Code. You can think of a function as a mini-program that runs within another program or within another function.
00:11 The main program calls the mini-program and sends information that the mini-program will need as it runs. When the function completes all of its actions, it may send some data back to the main program that’s called it.
00:25 The primary purpose of a function is to allow you to reuse the code within it whenever you need it using different inputs if required. When you use functions, you’re extending your Python vocabulary.
00:37
This lets you express the solution to your problem in a clearer and more succinct way. In Python, by convention, you should name a function using lowercase letters with words separated by an underscore, such as do_something()
, as seen on-screen.
00:55 These conventions are described in PEP 8, which is Python’s style guide, which you can check out in depth in this Real Python tutorial.
01:05
You need to add parentheses (()
) after the function name when you call it. Since functions represent actions, it’s best practice to start your function names with a verb to make your code more readable. In the next section of the course, you’ll see how to define functions with no input parameters.
Become a Member to join the conversation.