REVIT ARCHITECTURE (920) – PYTHON – Functions (4) – Parameterized functions (1)

A parameter is a variable with two important factors that make parameters different:

  • parameters exist only inside defined functions, and the only place where the parameter can be defined is in the space between a pair of parentheses in the def statement;
  • The value to the parameter is done at the time of the function’s invocation, by specifying the argument.

def function(parameter):

You should bear in mind that parameters live inside functions, but arguments exist outside functions, and they have values passed to corresponding parameters.

In the following function:

def message(number):

The definition of the function specifies that our function have just one parameter named number. It can be an ordinary variable, but as we have said,  only inside the function (it isn’t visible anywhere else).

We define more the function as the following example:

def message(number):
       print(«Enter a number:», number)
We have used the parameter. The value of the parameter will come from the environment of the function.

One requeriment of the definition of a function is to specify one or more parameters of the function. Also you must create as any arguments as parameters are defined. If you do not do that, there will be an error or mistake in the function.

If yoy invokes the function like this:

def message(number):
print(«Enter a number:», number)

message()

Probably you will have the following output:

TypeError: message() missing 1 required positional argument: ‘number’

So to invoke the function is better:

def message(number):
print(«Enter a number:», number)

message(1)

The code behave better, having the following output:

Enter a number: 1

The value of the argument used during invocation (1), has been passed into the function, setting the initial value of the parameter named number.

We can have a parameter’s function which have the same name as the a variable. The following instance shows this case:

def message(number):
print(«Enter a number:», number)

number = 1234
message(1)
print(number)

This case produces an action called shadowing:

A parameter shadows any variable of the same name, but only int he case when it(variable) is inside of the function defining the parameter.
In the other cases like the above example, the parameter named number is a completely different entity from the variable named number.

This means that the example of above will produce the following output:

Enter a number: 1
1234

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