Diferencia entre revisiones de «Tutorial:Funciones y Subrutinas»

De GeoGebra Manual
Saltar a: navegación, buscar
Línea 2: Línea 2:
 
title=Funciones y Subrutinas vía ''Guiones'' (''scripts'')
 
title=Funciones y Subrutinas vía ''Guiones'' (''scripts'')
 
}}
 
}}
Las [[:es:w:Subrutina|subrutinas]] (también denominadas funciones o procedimientos) involucran un concepto central en la programación y brindan una ayuda crucial en el mantenimiento de los programas dado que facilitan la reutilización.  En este tutorial se describen mecanismos para emplearlas en GeoGebra.
+
Las [http://es.wikipedia.org/wiki/Subrutina subrutinas] (también denominadas funciones o procedimientos) involucran un concepto central en la programación y brindan una ayuda crucial en el mantenimiento de los programas dado que facilitan la reutilización.  En este tutorial se describen mecanismos para emplearlas en GeoGebra.
  
 
'''Method 1.'''  [[Herramientas Propias|Herramientas Personales o ''Propias'']] act like a new function.  They can take inputs and produce output objects as a result.  You can invoke them from the toolbar, or from the input bar or scripts.   
 
'''Method 1.'''  [[Herramientas Propias|Herramientas Personales o ''Propias'']] act like a new function.  They can take inputs and produce output objects as a result.  You can invoke them from the toolbar, or from the input bar or scripts.   

Revisión del 14:26 17 feb 2014

Tutorial: Funciones y Subrutinas vía Guiones (scripts)

Las subrutinas (también denominadas funciones o procedimientos) involucran un concepto central en la programación y brindan una ayuda crucial en el mantenimiento de los programas dado que facilitan la reutilización. En este tutorial se describen mecanismos para emplearlas en GeoGebra.

Method 1. Herramientas Personales o Propias act like a new function. They can take inputs and produce output objects as a result. You can invoke them from the toolbar, or from the input bar or scripts. {{Note:Vewr también, para mayores detalles, el tutorial Preparando Herramientas Propias en la Barra.

Method 2. Any Javascript functions that you define in the Global Javascript section of the Scripting tab will be available to use from Javascript elsewhere in GeoGebra.

Method 3. Los guiones de GeoGebra no cuentan con respaldo para incluir subrutinas pero se pueden obtener los mismos resultados usando el comando EjecutaAlClic[].
Create an object, let's call it myObject, and enter some scripting code in myObject's On Click script. You can then invoke the code from other objects or from the input bar using RunClickScript[myObject]. If needed, inputs and outputs can be passed to the subroutine via additional objects.

Here's a more detailed example. Suppose that you have 5 points, A, B, C, D and E, and you want each point to turn red when clicked. One way you could achieve this is to write a separate 'On click' script for each point: for point A you would use SetColor[A,1,0,0]; for point B you would use SetColor[B,1,0,0], etc. If you decide to change from red to green later on though, then you must edit each point's script separately. If you have a lot of points then this can be tedious.

Instead, let's use a subroutine. Create a text object called target by typing

target = "A"

in the input bar. We'll use this object as an input for our function.

Next create a button called myButton. (You don't need to use a button; in fact, any kind of object should work just as well.) In myButton's 'On click' script, enter the following:

Execute[{"SetColor[" + target + ",1,0,0]"}]

Now go to point A and enter the following in A's 'On click' script:

target = A
RunClickScript[myButton]

Do the same for B, C, D and E, but replace the 'A' in the first line with 'B', 'C', 'D', 'E' respectively. Now when you click on any of the points A, B, C or D, it will change to red. If you decide that you want to use green instead, you only need to change the line in myButton's 'On click' and the change will take effect for A, B, C, D and E. If you have large number of points, then this method may be wiser than having to change each point's script separately!

How does this work? When you click on a point, 'B' let's say, then B's 'On click' script stores the name 'B' in the text object target. It then runs the subroutine that we stored in myButton's 'On click' script using the command RunClickScript[myButton].

myButton's 'On click' script now runs. The line Execute[{"SetColor[" + target + ",1,0,0]"}] constructs a GeoGebra command (this is the "SetColor[" + target + ",1,0,0]" part), and then executes it using Execute[]. The end result is that the command SetColor[B,1,0,0] gets executed, changing B to red.

© 2024 International GeoGebra Institute