Blockland Forums > Modification Help
Break; And Return;
jes00:
How do you use break; return; return true; and return false; and what are they used for?
SpreadsPlague:
break; is used in loops/switches to exit the loop/code block
for example, if we wanted to stop a loop for any reason before it reaches the rest of our code
--- Code: ---whatever loop here
{
//stuff
break;
//different stuff
}
--- End code ---
stuff would run, however break; would then be reached and different stuff would not run. in my opinion break; is much more useful in switches however they're not as simple as if statements so you don't see them as often.
return; returns a value, used for functions that .. return values. a function returning an integer for example could use "return answer;" where answer would be an integer value
for example if you wanted to set a variable "x" to y * z, you could write a function to multiply integers (which is completely pointless because you can do it directly, but for examples sake let's go with it)
I'm not actually familiar with Torque's syntax so I can't show you a Torque example, however here's one in java
--- Code: ---private int multiply(int y, int z)
{
return (y * z);
}
--- End code ---
now let's make variable x and tell it to become the result of multiply(y, z); .. the function is being used as an integer because that's it's return type:
--- Code: ---x = multiply(y, z);
--- End code ---
voila, x is now the result of y * z.
return true; returns a boolean (true or false) in this case true
return false; would do the same but return false
they are used in functions that return something (also called functions that have a "return type").
jes00:
Ok I now get break; but not return.
Ipquarx:
Return is used to end a function and give out a result.
Here is an example:
--- Code: ---function returnHello()
{
return "Hello!";
}
--- End code ---
Now, if you just do returnHello() in the console, it will do nothing.
But, if you do echo(returnHello()) in the console, it will say "Hello!", because the returned value is sent to the first variable in the function Echo.
Note that functions do not need to have a return, but it's generally a good idea to return errors if there was one.
CityRPG:
--- Code: ---function breakTest()
{
for(%a = 0; %a < 100; %a++)
{
if(%a == 20)
break;
echo("Loop is at:" SPC %a);
}
}
--- End code ---
Will echo 20 times. On the 21st time, when %a is equal to 20, it will hit the if condition and break.
--- Code: ---function sum(%a, %b)
{
return (%a + %b);
}
--- End code ---
This is the example typically used, but since just doing (%a + %b) will return the exact same result without the CPU overhead of a function call, it's a pointless impractical example.
Semantically, "get" functions are attached to objects to reduce redundant code. If you find yourself repeating the same 5 lines of code over and over again to get something from a related object, chances are you will want to make a "get" function. That way, if something changes later on, you won't be replacing tons of code.
For example, if we want to get the full player title, with clan tags, we'd need to do this:
%fullTitle = %client.clanPrefix @ %client.getPlayerName() @ %client.clanSuffix;
If you do this:
function GameConnection::getFullTitle(%client)
{
return %client.clanPrefix @ %client.getPlayerName() @ %client.clanSuffix;
}
Elsewhere, you can just do this:
%fullTitle = %client.getFullTitle();