(creating-functions)=
# Creating functions

We have introduced functions before in section {ref}`arguments-and-return-value-in-functions`. This time we will create our own.

But why?
1. To reuse our code somewhere else.
2. Make our code easier to read by *summarizing* a block of code with a meaningful name.

For example:
::::{grid} 2
:::{grid-item-card} Without functions
```cs
double[] dest1 = [3, 7], dest2 = [3, 4];
// Robot tool destination coordinates on a surface.

var distance = Math.Sqrt(Math.Pow(dest1[0], 2) + Math.Pow(dest1[1], 2));
Console.WriteLine($"Robot tool will move {distance:F2} m");

distance = Math.Sqrt(Math.Pow(dest2[0], 2) + Math.Pow(dest2[1], 2));
Console.WriteLine($"Robot tool will move {distance:F2} m");
```
:::
:::{grid-item-card} With functions
```cs
double[] dest1 = [3, 7], dest2 = [3, 4];
// Robot tool destinations

double Distance(double[] coordinates)
{
    return Math.Sqrt(Math.Pow(coordinates[0], 2) + Math.Pow(coordinates[1], 2));
}

Console.WriteLine($"Robot tool will move {Distance(dest1):F2} m");
Console.WriteLine($"Robot tool will move {Distance(dest2):F2} m");
```
:::
::::

:::{activity} Writing a function
Above example has still repetitive code. Create an additional function that gets rid of repetitive code.
:::

:::{activity} 
Write a function that returns the distance between two three dimensional points.

   - parameters: two points. Each point is an array of three numbers in meters.
   - return: a number in meters
   
<!--
2. Write a function that decides the gripper force based on the materials. Materials are:

   - steel
   - aluminum
   - plastic
-->

Choose a meaningful name for the function.
:::