Functions

One thing that made programming “Programming” was function. A function is a system. They are something like devices or tools which you create to do a specific task. For example hammer is a system and it’s function is to put a huge force to a special location. A pencil is a system that we have created to do the writing function.

In programming functions are so much like what I just told you! For example, all the commands we have in Arendelle such as p, r, d, w… all of them, they are functions too. For example, p is a system that does the painting function for us. We use it to paint the current dot.

You write the code for a specific job and then you use that code for the rest of your life without writing it over and over.

Think of it as something like this: I have a command that resets the location of my pointer and we all know it’s i. But there is no command to reset the color. So when I need to reset both color and location for many times. What should I do? For resetting the color I have this code:


[ #n > 0 , n ] 

It’s a code that research imagine you have to use this 20 lines of code in 30 different place of your code! Imagine even worse! You need to change this 20 line code that you have putted in 30 place of your code once in a hour! This is when you see you really need help! That’s why we have functions. Functions are blocks of codes, a bunch of codes that you give a special name so you can write them once and use them as much as you want. So like everything else functions are things starting with something! Like the sources that started with `#` and the spaces that prepended with `@`, functions got a `!` in their start! But they also got an `()` in their end. So a function is something like this:
!function()

You can also write spaces after the function’s name and the parenthesis. So something like this is accepted:
!function    (     )

We learn what `()` is for in this very chapter so calm down! Let’s continue with somethings first: Let’s see how we can build a function!
Functions are files. You know that you put your codes in a file with appended with `.arendelle` file format. We write functions into a file too. As you know from the past chapter. We then use **Arendelle File System**’s way of addressing to locate them. A function starts with `< >` in it’s top. We call `< >` function header. and then we write our code and then we save them in a `.arendelle` file as well so look at this example: We have this code:
[ #rnd * 10 , n ]

!resetColor ()

[ 5 ,

   [ 4 , pr ]
   [ 4 , l ]
   dn

]

!resetColor ()

[ 4 , pr ]

Then we save it on a file called `main.arendelle`:

Now we need to write the `!resetColor ()` function. But how can we write it? Simply we first need to create a file and call and it `resetColor.arendelle` If you’re using one of our Arendelle official IDE’s ( so called studio ) you have a function bar which you can add functions in them simply by clicking on the add function button. However we put the following code in our function file:
< >
[ #n > 0 , n ]

Now you should have something as the following:

So as you seen it, a function is another code that your code calls when it needs it. Like the **Stored Spaces** you have the Arendelle’s File Manager system here. You can write:
!folder.function ()

This is possible by creating a folder named `folder` and then put the `function.arendelle` file in it.

## More specifics about Arendelle File System's Path and Addressing in Functions or in short “A Note!” In any possible Arendelle code you run one file first. This file’s folder will be the **root** directory for the rest of the apps. Okay the fact is each file will be addressed comparing the main function (or what you may call main file). So for example if you create a main file and then you create a folder with two functions names `func` and `func2` you will have something like this:

Now as you see in the images each file has it’s own address based on the `main.arendelle`. So it doesn’t matter if you want to call `func2` from the `main.arendelle` or the `func.arendelle`. It’s address is `!folder.func2 ()` even if you try to access it from itself you have to use `!folder.func2()` So now you know every single tiny tricky mysterious thing about **Arendelle File System’s File Addressing**

## Function I/0 As you know a function is being written in another file, somewhere else and not in our main blueprint. So now you ask yourself: How can I tell something to that other files? I mean look at a function as a factory. A factory has an **Input** and an **Output**.
**Input** is the things the factory buys and uses to create it’s products and **Output** is the products. For example in the fork factory they buy metal as *input* an you change it to the a fork as *output*. But actually in the computers functions operate on **informations**: You give them some info and they use them to do stuff.

In the real world a factory always need input and output because without input how could a factory build something? A fork from air? And with no output there is no profit. So every single real world factory has come with this two attributes. Computer functions however are different. You do not have to have input but you can if you want. Look the first function we created:
< >
[ #n > 0 , n ]

It just changes the value of `#n` - nothing more. But there are some functions with input and output. For example: How you calculate the square root of a number like 125? You use this code:
( root , sqrt (125) ) 

Now, if you look `sqrt` is a function. I told you I show you what `( … )` is in the last of the function name and now I tell you. Its the input. You write the input/inputs between this two parentheses like when you enter `125` as input of sqrt. Entering a number to input in programming is called **Passing**. For example here I have passed 125 to the sqrt and as you see. The whole `sqrt(125)` is the output. Because if you look:
( root , sqrt (125) ) 

When Arendelle reads this and finds the `sqrt(125)`, it replaces the `sqrt(125)` with its result which is `11.18033988749895`. So if like before when you wrote:

#source + 2 / @space + $storedSpace

Functions work exactly the same. But you may ask how to create input and output for our functions in Arendelle?

For the input: You know that you have to write those two < and > in the beginning of your function code right? We use them for a simple fact. We write what inputs we want. For example let’s write a function that draws us a line in the given size, Something like:


!drawLine()

So when you use it, it be like:
!drawLine(10)

And it will draw you a line in the size of 10. But how? You know that you pass a number to the function right? The thing is you have to name this number in your function and save it as a space. So when someone passes a number, Arendelle save that number in a space for you and you be able to access it in the function. To do this, You have to name your input, How? You just have to write the name between the `< … >`. So if we start to write the `drawLine.arendelle` the code will be like:
< size >
[ @size , pr ]

Now if you write `!drawLine ( 10 )` and Arendelle reads the function, it will create a space names `@size` with the value of `10`. When it gets to the `[ @size , pr ]` it will draw a line in size of 10. If you also write a code like this:
( space , 1 )

[ 3 ,

   !drawLine ( @space ) d
   ( space , + 1 )

]

As you can see you get this image:


If you look at the code you see that in the look actually we have `!drawLine()` evaluated for 3 times like this:
!drawLine(1)
!drawLine(2)
!drawLine(3)

And if you look at the code of drawLine in this 3 times you see this: **First Time**
< size > // @size is 1
[ 1 , pr ]

**Second Time**
< size > // @size is 2
[ 2 , pr ]

**Third Time**
< size > // @size is 3
[ 3 , pr ]



## More than one input To pass more than one input to a function you have first know how does a function with more than one input looks like? Well, its too easy. We separate the inputs with “,” like this function that calculates a power of a number:
!pow ( number , power )

And to implement more than one input in the function’s blueprint we will write our `pow.arendelle` function header like this:
< number , power >

*But how to implement the output…?*

## Output Well output is the easiest part of the language! In Arendelle functions there’s one special space which Arendelle creates before it even starts to create the function itself (this creating thing in programming is called **Init** as its a short for initialization). This space is called **return**. If you look at it in a function you see that it has the value of zero and is inited. Return is one holy, special space. And actually the only holy space. You can’t create it, You can’t remove it. Only if you have it, you can change it’s values! But when the work of a function finishes, Arendelle reads the value of `@return` uses it as the output or shall we say **Result** of the function. So now let’s write the full `!pow(number, power)`:
< number , power >
( return , @number ^ @power )

So you see, it’s very simple! All you have to do is to put your result into the `@return` space. Now that you have your `!pow()` function, you can use it anywhere. Something cool is you get to use it in anywhere! Yes as you know before now you could only use sources in mathematical expressions and you could never could use commands in the mathematical expressions. However you can use function beside commands like:
[ 10 , pd ] !drawLine( 10 ) [ 10 , pu ]

But you also can use it in mathematical expressions. However, this time Arendelle will use functions as something like sources and spaces. So as before, it evaluates functions. But like the sources and spaces it will replace it with the value of `@return`. Now you want to have a line in size of 2 to the power of 4? Simply:
[ !pow ( 2 , 4 ) , pr ]




Exercise

Write a function like this:
!yourAgeInDays ( years , months , days )