Functions

Experienced developers will tell you that if you are copying and pasting while coding, then you are doing something wrong.

Repeating code is bad, both for debugging (because you have twice as many lines where mistakes can slip in), for readabiliy, and for maintainability.  You think you just cut and paste, but suppose you need to change the repeated code, then it is easy to miss a spot.  In other words, it’s error-prone. 

Functions are bits of reusable code that let you write programs more cleanly.
They are not the same as mathematical functions, they are chunks of code that perform any task you want.

  • They help you avoid repeating yourself (🔁 DRY: Don’t Repeat Yourself).
  • They keep your code easier to read, fix, and reuse.
  • They reduce errors caused by accidental copy-paste edits.

Remember: you’ve already used functions like print(). Those are built-in. You can create your own functions with the def keyword.

It is  good practice when you spot repeated code to wrap it in a function and call the function wherever you need it.

You have completed 0% of the lesson
0%