HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

I don't fully understand the RETURN statement

03-01-2003, 06:09 PM#1
FyreDaug
I only understand that it's the same as "Skip Remaining Actions" in the GUI.

But why does it do what it does? How do you define return?

If you have something like:
If x=2 then do set x = 3 else Return
Would that end the entire code or just that function in the trigger?

Also you can do Return (Statement) how does that work?

Thanks alot guys, your a big help.
03-01-2003, 09:06 PM#2
Guest
Quote:
I only understand that it's the same as "Skip Remaining Actions" in the GUI.

But why does it do what it does? How do you define return?

Return is a common syntax used in almost all programming langauges. It's basic function serves as an intentional end of a function excution(which some might consider bad programming practice), as well as return the result of a computation to the calling function.

Quote:
If you have something like:
If x=2 then do set x = 3 else Return
Would that end the entire code or just that function in the trigger?

It will end exactly where the Return is excuted. Further lines within the function are ignore.

Quote:
Also you can do Return (Statement) how does that work?
As mentioned above, the Return can be used to return the results of a computation back to the calling function.

As for what sort of values are returnable are usually dependent on the types allow my the language. But common types are integers, floats, string, character, pointers.

Example, you got 2 functions. The first one is the callin function n the 2nd one is juz a simple add function.

function CALLING_Function takes nothing returns nothing

local integer udl_a // declare local variables
local integer udl_b
local integer udl_Result

set udl_a = udg_User_Input_1 // get user input 1
set udl_b = udg_User_Input_2 // get user input 2

set udl_Result = ADD_function( a, b ) // call Add_function and
// store the RETURNED value
// in udl_Result

...................other statements..................
...................other statements..................
// Do whatever thing with the RESULT, display or use it for
// next decision making

endfunction

function ADD_function takes integer a, integer b returns integer

return ( a + b ) // return the results of a + b

endfunction


Hope this sort of giving u some idea on the uses of RETURN.

P.S thx for the webspace, will upload in a few days time : P

o yeah check out http://jass.sourceforge.net too, it got a nice basic introduction to the JASS language.
03-01-2003, 09:09 PM#3
Guest
sorry for the terrible placements for the sample code @@.....not used to the spacin on this forum yet : P.
03-03-2003, 12:37 AM#4
FyreDaug
you can use [code] and then end it and it will format it the way you have it.

1 more question,

Quote:
return ( a + b ) // return the results of a + b

I still don't understand that, it returns it where? If a was 1 and b was 2, would it print 3 on the screen or something?
03-03-2003, 01:11 AM#5
Nozdormu
If the function has a return type, you're usually returning the value to a variable, at least in most languages.
03-03-2003, 01:25 AM#6
Guest
Iv got an easy explanation for you

Sample function:

Quote:
function main takes nothing return nothing
{
local integer x;
x = call example;
}
function example takes nothing returns integer
{
local integer a = 1;
local integer b = 2;

return(a+b)
}

In this you have the function main. It declares a local integer x, then assignes it to run the function example. Example is then run, as you can see it returns an integer, this means that you can only use the return statement for integers. a is assigned 1 and b is assigned 2 then there values are added (3) and returned.

Now the return value of example is 3. This value is assigned to x. So if you return an expression (such as a+b) that is bascally the value of the function.

Consider this:
Quote:

//this is c++ code
#include <stdio.h>//dont worry about this

int example(int what_to_print)
//jass : function example takes int what_to_print returns integer
{
return(what_to_print+5);//you can figure this one out
}


int main()
/*Jass equivilent: function main takes nothing returns integer*/
{
int x;//jass : local integer x

x = example(5);//jass: x = call example

printf(x);//dont know in jass:)

printf("\n");//prints a new line
printf(example(5));

return 0;
}

output:

10
10

In this code the functions are self explanitory. What happens is that we assign x to example(5), this calculates to 5+5 because thats what we returned from example. then we printed a new line, then we directly printed the return value of example(5) so you can see that it evaluates to 10 (5+5).

This might be a little confusing but read it a few times and it should help you.

And by the way, skip remaining actions in gui is equivilent to just

return;

with no value.