# Functions

## Function Declaration

A function declaration introduces the function name and its prototype. A function definition associates the function name with the function body.

### Synopsis

```
(func (<function_name> (<function_1st_parameter>) (<function_2nd_parameter>)) {
    <function_body>
})
```

Function parameters must be a valid [**type**](https://glados-4.gitbook.io/c/data-types)**.** The value returned is the last statement in the function body.

### Example:

```
(func (strlen (char * str))
{
    (int i = 0);
    (while ((!= (str[i]) '\0')) {
        (i = (+ i 1))
    })
    (i)
})
```

In this example, the function name is `strlen`. This function takes a string as **parameter** and return the length of it.

### Recursive calls&#x20;

```
(func (fib-it (int a) (int b) (int n))
{
    (int ret = 0);

    (if (< n 1) {
        (ret = a);
    })
    (if (>= n 1) {
        (ret = (fib-it b (+ a b) (- n 1)));
    })
    (ret)
})
```

Here an example of the Fibonacci function with the function `fib-it` that is called **recursively**.

## Builtins

The C-- language provide some builtin functions:

| Function Name | Description                                                                                                                 | Prototype                                 |
| ------------- | --------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- |
| malloc        | The `malloc` function allocates size bytes and returns a pointer to the allocated memory.                                   | char \* malloc(int size)                  |
| write         | `write` writes up to `size` bytes from the buffer starting at `buffer` to the file referred to by the file descriptor `fd`. | int write(int fd, char \*buffer,int size) |
| puts          | `puts` writes the string `str` and a trailing newline to stdout                                                             | int puts(string str)                      |

### Examples

#### Malloc

```
(func (up (int a)) {
    (a + 1);
})

(func (ups (int * h)) {
    ++(h[0]);
})

(char * str = malloc(4))
(int i = 0)
((str[i]) = 'J')
ups((&i))
((str[i]) = 'B')
((str[up(i)]) = 'D')
```

In this case, we are manipulating pointers after using the malloc function to create a char \* with the characters "J" "B" and "D" in it. The size of `str`  is 4 because of the malloc.

#### &#x20;Write

```
(int i = 48)
(char c = ((char)i))
write(1 (&c) 1)
```

We define an integer `i` with the value 48 and **cast** it in the [**char**](https://glados-4.gitbook.io/c/data-types#char) type. After that, we use the `write` function to display the character in the standard output by sending the address of it since the function takes a [**pointer**](https://glados-4.gitbook.io/c/data-types#pointer-types)**.**

#### Puts

```
(string str = "ollamy")
puts(str)
```

We define a [**string**](https://glados-4.gitbook.io/c/data-types#string) `str` and we use the function `puts` to display it in the standard output.

### Rewriting C builtins

Here are some example of implementations of function made during the Epitech C pool with the C-- :

#### Putchar

```
(func (putchar (char c))
{
  write(1 (&c) 1)
})
```

#### Putnbr

```
(func (putnbr (int n))
{
    (int tmp = 0);
    (char c = '0');

    (if (n < 0) {
        putchar('-')
        (n = (n * -1))
    })
    (if (n >= 10)
    {
        (tmp = (n // 10))
        putnbr(tmp)
        (tmp = (n % 10))
        (tmp = (tmp + 48))
        (c = ((char)tmp))
        putchar(c)

    })
    (if (n < 10) {
        (tmp = (n % 10))
        (tmp = (tmp + 48))
        (c = ((char)tmp))
        putchar(c)
    })
    (n)
})
```

#### Putstr

```
(func (putstr (char * str))
{
    (int a = 0);
    (while ((str[a]) != '\0') {
      putchar((str[a]))
      (a = (a + 1))
    })
    (1)
})
```

#### Strlen

```
(func (strlen (char * str))
{
    (int i = 0);
    (while (((str[i]) != '\0')) {
        (i = (i + 1))
    })
    (i)
})
```
