# Tutorial:Scripting:Functions and subroutines

Tutorial: Functions and subroutines in Geogebra scripting

Subroutines (also known as functions or procedures) are an important concept in computer programming, and help with program maintenance and code re-use. This tutorial describes some ways that you can enjoy some of the advantages of subroutines in GeoGebra.

Method 1. Custom tools 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. See also the tutorial on custom tools.

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. GeoGebra Script does not provide built-in support for subroutines, but we can achieve the same results by using the RunClickScript[] command. 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.