C tutorial

Part 2


RETURNING 0

C is still old and weird. Let's see an example:

int main(void) {
  return 0;
}

Wonderful. This one is a function. It returns 0. That's it. But there are important things to look at. Piece by piece.

First is "int" which is short for integer, this tells you what type of "thing" it is. Here it represents the return type, this is the type of value that the function returns.

After the type, you have the name, in this case, "main", and in this case it is a function, "main" is a special name for where the code starts. Your code executes first in main. Your computer does some setup before this, but YOUR code starts there. Main usually goes into other functions, in this instance it doesn't.

You can tell it's a function because of the parentheses after the name. A function always has parentheses after it. Inside parentheses, here, are parameters, types that must be supplied to the function, also, order matters. Here, the only thing in it is "void". Void is another type, just like int, and void represents... "nothing". In this case it means that you should not supply any arguments. You do not need to name the parameters, you need to specify their type, but if you don't name them then you can't access them and here void is a little special other than a type but the specifics don't matter for like anyone. Here we don't want to use them so they are unnamed.
Fun fact, if you put nothing in between the parentheses, calling the function lets you put arguments even if none are specified. This makes absolutely no difference*.
The "signature" of a function is the return type, the name, and the parameters.

Then is the function body, this is the code inside the function if it wasn't obvious, these are inside braces (aka curly brackets or whatever you want to call them; I will call them braces henceforth and forevermore). The only thing actually inside of it is "return 0;" which is a statement. All statements end in a semicolon. "return" means the function wants to return a value, and, if we look at the first word, we are returning an int. 0 is an int. Splendid. If you remember how void represents "nothing", you can also set the return type to void. In that case you would return nothing and you would have return and then the semicolon anyway. This isn't that. Return exits the function, the function ends at return, even if there is code below it.

A function is a piece of code with a name, a "package" of code if you want to think of it that way. You can call them, and doing so runs the code.
All of this will become clearer over time so don't worry if it's a little weird.

* Unless you're geriatric working on Unix. It hasn't mattered since the 80s, and if you are using C23 or higher then there is literally no difference. Practically there isn't a difference even if you aren't using C23 unless you do un-prototyped forward declarations which is not something you should do. Is it bad to put nothing between parentheses? You decide. You don't have to put anything in the parentheses if you don't want to, but if you want to require no values to be passed, you can put void. It is not mechanically the same thing but for you or most people it's negligible.