Okay, i’ll give it a try. I’m German though, can’t tell you all the correct technical terms.
The following line is a simple variable:
int myInt = 5;
The first thing is the type, the second is the name. Moreover there can be modifiers for different uses.
I think that’s clear, now let’s start with a basic function in C#.
Functions
void SayHello()
{
Debug.Log("Hello World");
}
A normal function helps to write re-usable code, you can call it in your code with a single line so that you do not need to re-write all the code which you put into the function’s ‘body’. Additionally, the list of paramters makes it even modular and re-usable even more, which is a great thing you would not want to miss once you understood the principle. Believe me, it saves lot’s of lines => work => time, even in small projects.
Made a logical mistake? A calculation was implemented wrong? Maintain your function and it will most-likely be fine. That’s actually the best thing, you don’t need to waste time searching all the mistakes of copy/paste code.
Return type
Now, the first thing a function usually has (there are special ones such as constructors which do not have one) is the return type. This can be void, int, float or any other basic or your own type (data/reference types → see below).
In my example, it is void, so nothing will be returned and the return-statement is not neccessary.
The function does it’s task and whenever it’s done, the programm will continue at the point at which it has jumped ‘into’ the function.
If you choose to have a return type, the function needs to have the return-statement and a valid variable or a constant ‘hard coded’ value. (in German it’s also called Literal, not quite sure whether it’s the same in english or not).
With a return type, the function can be used similar to variables.
- assign the returned value to a variable
- use it directly, i.e. in a calculation
Name
Second part is the name. You should always take self-descriptive names so that other people who take a look at your code can follow along alot easier + it will help you alot, too.
List of parameters
The third part which seems to be the most confusing to you is the list of parameters. Those are local variables of the function which will only be available in the function itself! The above example hasn’t got any paramters, so let’s add one.
void SayHello(string name)
{
Debug.Log("Hello " + name);
}
Now, whenever you call this function, you will need to pass in a valid variable of the type string or some ‘hard coded’ string as in my example below. (Okay, there’s still overloading, but that rather counts as intermediate).
Let’s say you call it in the start function
void Start()
{
SayHello("Suddoha");
}
The string will be passed to the function and the function will do whatever is defined in its body.
One thing you need to know by now is that there basically two different types of variables:
In C#, types such as int, float, bool, any kind of structs etc are data types which means they ‘really’ contain data.
Reference types are variables which actually contain a reference to an instance of a class,it’s a bit different there.
Right now it’s enough to talk about data types in order to understand the code you posted.
Data types:
If you were about to assign an int variable to another int variable, you would actually copy the value it contains. Thus you can alter the second int without changing the first int.
That’s what happens in your example. An integer is passed to the function and can be accessed by ‘number’, actually it will be copied this way and using 'number’ will not affect ‘myInt’ The function multiplies ‘number’ by two. At this point, ‘myInt’ is still 5, ’ number’ is 10 - remember, integers are data types and thus treated independantly. (Ofc, there’re ways to make this work differently, but that’s far away from beginner skills).
Since the purpose of that example script is the calculation and reassignment of the class variable myInt, you need to use the value which is returned by the function. Remember, we can use the function just like a normal variable in this case.