Author Topic: Break; And Return;  (Read 1049 times)

How do you use break; return; return true; and return false; and what are they used for?

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: [Select]
whatever loop here
{
    //stuff
    break;
    //different stuff
}

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: [Select]
private int multiply(int y, int z)
{
    return (y * z);
}


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: [Select]
x = multiply(y, z);
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").
« Last Edit: January 12, 2012, 09:13:52 AM by SpreadsPlague »

Ok I now get break; but not return.

Return is used to end a function and give out a result.

Here is an example:
Code: [Select]
function returnHello()
{
    return "Hello!";
}
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.

Code: [Select]
function breakTest()
{
for(%a = 0; %a < 100; %a++)
{
if(%a == 20)
break;

echo("Loop is at:" SPC %a);
}
}

Will echo 20 times. On the 21st time, when %a is equal to 20, it will hit the if condition and break.


Code: [Select]
function sum(%a, %b)
{
return (%a + %b);
}

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();
« Last Edit: January 12, 2012, 09:43:01 AM by CityRPG »

Return is used to end a function and give out a result.

Here is an example:
Code: [Select]
function returnHello()
{
    return "Hello!";
}
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.
So if I did
Code: [Select]
function Blah(%client)
{
         if(%client.fudge == returnTest())
         {
                  //Nothing.
         }
}

function returnTest()
{
         return 7;
}
Then it would be the same as doing
Code: [Select]
function Blah(%client)
{
         if(%client.fudge == 7)
         {
                  //Nothing.
         }
}
If so return is in my opinion pretty useless.

You're not getting it.

This is a code snippet from CityRPG:

Code: [Select]
// Fetches a player's profile (creating it if nessicary).
// @ProfileSO
function GameConnection::getProfile(%client)
{
if(!Profiler.isProfileActive(%client.bl_id))
if(Profiler.isProfile(%client.bl_id))
if(Profiler.loadProfile(%client.bl_id))
return Profiler.getProfile(%client.bl_id);
else
return -2;
else
if(Profiler.createProfile(%client.bl_id))
if(Profiler.loadProfile(%client.bl_id))
return Profiler.getProfile(%client.bl_id);
else
return -2;
else
return -1;
else
return Profiler.getProfile(%client.bl_id);
}

In my code, this function is called over 100 times throughout almost 30,000 lines of code. Imagine having to replace every single instance of %client.getProfile() with that monster I quoted.

Also notice how each if() has only a function called in it.  These are all returning true/false based on success. Is it active? Existing? Can we load it? Can we create it? Each function also has more is() and get() checks inside of it. There's literally hundreds of contingent returns in play here.
« Last Edit: January 12, 2012, 09:57:01 AM by CityRPG »

For every cell in your body, there are 20 returns in CityRPG.

For every cell in your body, there are 20 returns in CityRPG.
more like 50.
No im joking, more like .5 really.
Do you know how many cells are in the human HAND alone?

more like 50.
No im joking, more like .5 really.
Do you know how many cells are in the human HAND alone?
Yeah. Not enough to fulfill all the returns used in CityRPG.
NEVER ENOUGH

normaly i use return like break, but for the whole function
i don't tend to actualy return values, although it would be useful for helper functions