Functions

Advantages of functions

Functions are fundamental to programming and a very important programming feature that you will need to learn and understand. A problem that looks impossible at first to program becomes much easier once you break down the problem into smaller 'chunks'. These smaller 'chunks' then become your functions in your program.

Advantages of functions

  • Reduces the repetition of code within a program – function allows you to extract a commonly used block of code into a single component. Now you can perform the same task by calling this function wherever you want within your script without having to copy and paste the same block of code again and again.
  • The code is much easier to maintain – since a function created once can be used many times, any changes made inside a function are automatically implemented at all the places without touching the several files.
  • It is easier to eliminate the errors – when the program is subdivided into functions, if any error occurs you know exactly what function is causing the error and where to find it. Therefore, fixing errors becomes much easier.
  • Can be reused in other applications – because a function is separated from the rest of the script, it's easy to reuse the same function in other applications just by importing the Python script containing those functions.