even though you do it twice so the if statement doesnt make any sense.
also, you try to create the jump variable twice inside if block and return it outside the if block.
double fRangeLength(double start, double end, double length){
// Function context
if (start > end) {
// if true context
double jump = -length;
} else {
// if false context
double jump = -length;
}
return jump;
}
If you declare a variable in a if-true-body, you just can use it in that context. In the same way, if you declare a variable in a function, you can not access it from a parent context.
I have a curiosity question. Why is the public void with all functions and stuff below the private void?
So, by that, different from Python, C# first reads all the code and then starts working with it in the private void while in Python you have to place all functions before.
Is it correct that C# first reads all the code and then gonna work with it in the private void?
private or public or protected are access modfiers. The are used to constrain the context of the calls. For example, if RunScript() is private, that means that you can not call Script_Instance.RunScript() from an upper context, just inside the class. Protected is similar, but the call can be done by a derived class.
void is a reserved word for methods that doesnât return anything.
Well, first the precompiler read all the code to check that can be compiled without syntax erros. Do you mean that?
Its never wrong to stick to conventions people working on these things for decades have thought of(and yes, differing from them is also ok). No reason to make code harder to read.
As others have pointed out, if you declare something within brackets, the definition is valid only inside that bracket (or âcontextâ). So the following makes the âjumpâ variable isolated (isnide the if clauses brackets):
public double fRangeLength(...)
{
if (true)
{
// jump is "visible" only to code within these *nearest* brackets
double jump = - 1 * length;
}
else
{
// Same here, the "jump" variable above is unkown here, and declaring
// it anew won't make it visible outside of these brackets either:
double jump = -length;
}
// So, jump is unknown here! (again, its known existence is isolated
// between the nearest brackets in which it was declared)
}
Therefore, make sure you declare âjumpâ between the brackets which encloses the entire function, then it will be known within en entire function, like so:
public double fRangeLength(...)
{
double jump = 0.0; // <-- Here! Now "jump" is known/visible until the
// function's corresponding closing bracket far below
if (true)
{
jump = - 1 * length; // jump is "visible" here
}
else
{
jump = -length; // same here, jump is "visible" also here
}
// and it follows, "jump" is known/visible also here, even all the
// way down to the function's closing bracket!
...
...
...
...
} // <- jump is visible all the way down to here