-
Book Overview & Buying
-
Table Of Contents
Python Illustrated
By :
Whenever you find yourself writing the same or very similar lines of code multiple times at different places in your code, a function can prevent that. By wrapping code in a function, you can call it with just one line. This means that you’ll have less duplicate code. And that’s great! Imagine if, at some point, we’d have to change that chunk of code that appears multiple times in multiple places across our code… Yup, we’d have to change it in all those places! It’s easy to miss one and a lot of work.
That’s why functions also bring organization. Instead of scattering logic throughout your file, you can place each piece of logic in a function with a descriptive name. Then, if you need to change it, you do so in one place (the function itself), and the change applies everywhere the function is called.
We have already used functions! Python has built-in functions itself as well, to mention just a few of...