REVIT ARCHITECTURE (917) – PYTHON – Functions (1) – Introduction

In this article I define the first condition which can help you decide when to start writing your own functions in Python:

  • if a particular piece of the code begins to appear in more than one place, you can consider the possibility of isolating it in a function invoked from the points where the original code was placed before.

It could happen that an algorithm you’re going to implement is so complex that your code begins to grow in an uncontrolled manner.

You can try to cope with the issue by commenting the code extensively, but too many comments make the code larger and harder to read. We recommend you a well-written function that should be viewed entirely in one glance.

So the advice is to divide the large code  into well-isolated pieces, and encodes each of them in the form of a function.

This considerably simplifies the work of the program, because each piece of code can be encoded separately, and tested separately. The process described here is often called decomposition.

In general, functions come from at least three places:

  • from Python itself. We call these functions built-in functions.
  • from Python’s preinstalled modules. The use of these functions requires some additional steps from the programmer in order to make them fully accessible.
  • directly from your code. You can write your own functions.

print(«Enter a value: «)
a = int(input())

7139

Licencia Creative Commons Contenido Web de Yolanda Muriel está sujeto bajo Licencia Creative Commons Atribución-NoComercial-SinDerivadas 3.0 Unported.

Deja un comentario