Functions
Functions are one of the most beautiful things in programming. They let you take some piece of code and give it a name. Functions make it possible to split big problems down into little pieces, and then let you reuse the parts of your solution to solve other problems.
Thinking in functions will be different from the programs you've written so far. Instead of thinking about input and output of your program (with input
and print
), you'll think about the input and output of your functions: arguments and return values. Instead of planning all the steps your whole program needs, you'll think about which functions it needs, and how those functions work together.
Topics covered
Here's a summary of what you'll learn in these lessons:
- Functions allow us to write some code once in our program, then run it as many times as needed.
- Functions are used (i.e. called or invoked) by writing the name followed by parentheses, with the function's arguments in the parentheses.
- Python has many built-in functions, like
print()
,str()
, andrandom.randint()
- You can find available functions and modules by looking at the Python docs or searching Google
- Python lets you define your own functions using the keyword
def
return
sends back a value from a function