REVIT ARCHITECTURE (918) – PYTHON – Functions (2) – To create a function

With the purspose to make a function, you should define it.

The simplest function definition looks like is for instance:

def function_name():
function_body

The main features of a function are:

  • It always starts with the keyword def (that means define)
  • After def it is placed the name of the function (the rules for naming functions are exactly the same as for naming variables).
  • After the function name, there is a place for a pair of parentheses.
  • This sentence has to end with a colon.
  • After the sentence Name function, begins the function bod, at least one nested instructions, which will be executed every time the function is invoked; You should bear in mind that the function ends where the nesting ends, so you have to be careful.

One example of function is:

def information():
       print(«Enter a value: «)
In this case is not invocation of the function’s code because we do not use the function:
def information():
      print(«Enter a value: «)
print(«We start now.»)
print(«We end now.»)

When you run it, you see the following output:

We start now.
We end now.

Python read the function and remember it, but it do not launch the function without our permission.

In the following example there is an invocation of the function:

def information():
print(«Enter a value: «)

print(«We start now.»)
message()
print(«We end now.»)

The output looks different now:

We start now.
Enter a value:
We end now

https://platform.twitter.com/widgets.js

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

Deja un comentario