Exercise 18. Names, Variables, Code, Functions
Big title, right? I am about to introduce you to the function! Dum dum dah! Every programmer will go on and on about functions and all the different ideas about how they work and what they do, but I will give you the simplest explanation you can use right now.
Functions do three things:
1. They name pieces of code the way variables name strings and numbers.
2. They take arguments the way your scripts take argv
.
3. Using 1 and 2, they let you make your own “mini-scripts” or “tiny commands.”
You can create a function by using the word def
in Python. I’m going to have you make four different functions that work like your scripts, and then I’ll show you how each one is related.
Get Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.