27 lines
1.3 KiB
Markdown
27 lines
1.3 KiB
Markdown
|
Rolling one dice isn't that exiting. Every side has the same chance. Now we want to roll two dice at the same time without writing the same code all over.
|
||
|
To do that, we use **functions**.
|
||
|
|
||
|
## Knowledge
|
||
|
|
||
|
Functions are like subprograms, which you can call. They look just like our main program.
|
||
|
You actually called functions before. Remember those more complex instructions like ``printf`` and ``rand``.
|
||
|
They were all functions.
|
||
|
|
||
|
The first thing you have to write, is the **return type**. The return type tells the computer, what kind of value you function calculates and returns.
|
||
|
For our dice example this would be an **int**. There are also functions which return nothing. In that case the return type is **void**.
|
||
|
|
||
|
Next comes the name. You can name your functions any name you like with a few restrictions.
|
||
|
After the name you write the **arguments** in parentheses. They also have a type and a name. The dice function doesn't need any arguments.
|
||
|
Between the curly braces comes your code. In the end you have to write **return** and behind that the value your function calculated.
|
||
|
|
||
|
Here is an example of a simple add function:
|
||
|
|
||
|
int add(int a, int b)
|
||
|
{
|
||
|
return a+b;
|
||
|
}
|
||
|
|
||
|
## Task
|
||
|
|
||
|
Your task is to implement the die function and call twice to simulate the rolling of two dice.
|