Functions
Tags: c, beginners
Published: 2019-06-13 18:31:08

Awesome, we get to talk about functions, and after that, we can finally start to write a real code!

In my notebook, I made a comparison between functions and factories. So, let's talk about factories. (Yes the real world things with machines and workers).

Let's imagine we have a coffee machines factory. That means that our lovely and successful factory buy cables, some metal, and plastic (and perhaps some more raw materials) and from all that it produces coffee machines. A lot of them.

Ok, so in other words, our factory gets some raw materials (cables, metal, plastic) as input and produces the coffee machine as output.

A coffee machine is a factory too. Its input is a coffee capsule, water, and milk (optional) and its output is coffee. And coffee is input for a programmer, who produces some code and a lot of bugs as output.

In conclusion, we understand that we surrounded by factories/functions on a daily basis. A function is just a block of code (so yeah, it's bounded by curly brackets) which can get variables as inputs (in the example above: raw materials) and returns output according to its type.

Simply put, the function format is:
```cpp
[output_type] [function_name]([inputs]) {
    // some code
}
```
The inputs format is:
```cpp
([type1] [variable1], [type2] [variable2], ...)
```
The function of our coffee machines factory may be:
```cpp
coffeeMachine makeCoffeeMachine(cable c, metal m, plastic p) {
    coffeeMachine newMachine;
    build(&newMachine, c, m, p);
    return newMachine;
}
```
And you can see that our function calls another function: "_build_". This function builds the new coffee machine somehow (we don't really care how) and then we can return it as output.

Good, now let's talk about real functions. The first function we will talk about is "___main___". The ___main___ function it is the most important function in your code. If you don't have one (and only one) "the computer" won't let you to run your code. The readers who were curious enough probably already saw some _main_ functions before. Let's write one by ourselves:
```cpp
#include <stdio.h>

void main() {
    int x = 10;
    int y = 5;
    if (x > y) {
        printf("The max number is %d\n", x);
    } else {
        // y is equal or higher than x
        printf("The max number is %d\n", y);
    }
}
```
As you can see, our function initializes two variables and then prints the sum of them. Let's build some more function, which helps the _main_ function to be more compact:
```cpp
#include <stdio.h>

/*
* The function gets 2 integers and returns the max value
*/
int max(int a, int b) {
    if (a > b) {
        return a;
    } else {
        // b is bigger or equal
        return b;
    }
}

/*
* The main function
*/
void main() {
    int x = 10;
    int y = 5;
    printf("The max number is %d\n", max(x,y));
}
```
As you can see, our new function ___max___ gets two integers as input and returns the max value as output. Notice that we can name the variables of each function in any way we want. The only thing that's important it's the order of the variables. In the example above, the "box" we named 'x' in ___main___ copied its value to another "box", which we named 'a' in ___max___. Notice, it's not the same "box" (variable) but it's the same value. We will explain it more clearly when we get to talk about passing variables the functions __By Reference__.

As you can see the ___main___ function above gets no inputs, but it can get arguments this way (we will not dwell on this now):
```cpp
int main(int argc, char **argv) {
    // code

    // Success
    return 0;
}
```

Moreover, our _main_ doesn't return any output, and there its output type is ___void___. In general, if we want to write a function which returns nothing we declare it with a _void_ at the beginning. That means that ___void___ function can be very useful as a printing function or as a reading data from the user (or from a file) function. In the near future, we will learn how a function can also manipulate variables, without a need to return them (Hint: the __By Reference__ thing again).

Notice that the ___main___ function in the last example above returns an integer as output. This is in accordance with the convention that if the ___main___ function finishes successfully it returns 0. It can also return something else according to what we decide. For example, negative numbers are usually returned when an error has occurred and we don't want to proceed.

####__Always remember, it's much C-mpler than you thought!__

Regards,
Gilad