>>1
A function takes some work to understand. I'm not sure what parts you're missing, so I'll explain the whole thing.
A "function" is just a piece of code that you can reuse. This allows you to write a bit of code once and then use it multiple times. There are two parts to using a function: the place where you tell the computer what it is, and the place where you tell the computer that you want to run that code. The former is called a function "declaration," because you are declaring your intent, I suppose. The latter is called a function "call."
The function declaration has four main parts. These are, in order (I'll explain them one by one)
1. return type
2. function name
3. arguments
4. function body
an example:
int main(int argc, char** argv) {
return 0;
}
here, the first word, "int" is the return type. This function returns an integer. This means that when you call the function it will look like this:
int result;
result = main(0, 0);
Never mind the part in parentheses (it's not exactly valid): the thing to look at here is that you get an integer "out of" the function.
Next, the function name - in this case it's "main." Easy enough. When you call it, you call it by name.
Third, function arguments - these are the things in (). Note that in the function declaration you have to provide both a type and a name for each argument, as if you were passing in a variable. For example, int argc is an integer with the name "argc." If you don't know what functions are yet, you probably also don't know about pointers, so don't worry about what a char** is (until later).
Why arguments? Imagine a function to add two numbers together:
int add(int first, int second) {
return first + second;
}
This is a stupid function. Don't write it. But it
is an example. You can see that you can pass in any numbers to get them added together:
int a, b;
a = add(2, 5);
b = add(a, a);
In this example, a would be given the value 7 (2 + 5), and b would have 14 (a + a or 7 + 7).
Finally, the function body. Obviously you've seen this by now - it's everything between the two { } curly brackets, or "crazy brackets" as my novice programmer friend calls them. You can do all sorts of things here: you can have many lines of code, and do lots of stuff. At the end, you write
return [...]
where [...] contains whatever you want to return. It should be of the type you defined as your return type, obviously.
Good luck. C is hard.