HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Ok

01-10-2007, 05:20 AM#1
Feroc1ty
i got basics of jass down, which i create a basic functions without using takes x returns y, now iwana know how to do those, i tried to make a basic script but failed.

Collapse JASS:
function even takes real r returns boolean udg_k
    local integer i
    set i = ModuloReal(udg_r, 2.00)
    if i == 0 then
        return udg_k(TRUE)
    else
        return udg_k(FALSE)
    endif
endfunction

i know its prolly all way wrong can someone tell me how to a basic one of those and explain how do takes and returns stuff works or get a link to tutorial?
01-10-2007, 05:24 AM#2
Rising_Dusk
Yes, it does have some syntax errors.
It should look like this --

Collapse JASS:
function IsEven takes real r returns boolean
    local real mod = r - I2R(R2I(r / 2.)) * 2.
    if (mod < 0) then
        set mod = mod + 2.
    endif
    return mod == 0
endfunction

I saved a line and combined the two 'i' references.
Also, the real problem was that you return a data type, not a variable.
So as you can see, it just returns boolean, not boolean udg_k.

You can also skip the conditional if/then/else and just directly return.
That saves lots of lines and looks prettier.
Either way, you need a return at the end.
WC3's syntax checker requires it.

Also, not everything needs an "udg_" on it.
Those are only for User Defined Globals, like the ones in the variable editor in the trigger editor.

ModuloReal() is a BJ and that it returns a real and not an integer.
I also shoved the entire BJ stuff into it to save a function call.
This is about as good as I can say you could get it.

Although, if you still want to use the BJ, it would look this way.
Collapse JASS:
function IsEven takes real r returns boolean
    local real mod = ModuloReal(r, 2.)
    return mod == 0
endfunction

That's pretty much what was wrong.
01-10-2007, 02:24 PM#3
Feroc1ty
Alright, but i dont quite understand these parts.

what does it do when it says take "type" "name"

is it when function is called and it has (xxx) there?
and how does the return work?

and why did you convert a real into a integer, and than convert it into real yet again.
01-10-2007, 04:57 PM#4
Rising_Dusk
Quote:
and why did you convert a real into a integer, and than convert it into real yet again.
Truncation of the decimal places.
That's how ModuloReal() works.

And you take a datatype and a variable name so that you can reference the variable in the script.
Collapse JASS:
function Something takes integer i returns real
    local integer otherint = i + 10
    return I2R(otherint)
endfunction
While that does nothing useful, it demonstrates the point.
If you didn't give it a datatype AND a name in the "takes" portion of the function, you wouldn't be able to reference it in the function code and then compare it with other values, etc.

So if it says "takes integer i", then that means you need to supply an integer argument to the function when calling.
So you would do for example --
Collapse JASS:
call Something(10)
Or --
Collapse JASS:
local real r = Something(10)
Because it does return a real.
01-11-2007, 12:33 AM#5
Feroc1ty
so on return mod == 0, i dont get that, if its not equal to 0 it returns false, and if it does it returns true?
01-11-2007, 02:18 AM#6
Rising_Dusk
Quote:
if its not equal to 0 it returns false, and if it does it returns true?
Correct.
01-11-2007, 02:23 AM#7
Ryude
Modulo can only returns two things, 0 and 1. If it's 0, then it's even, if it's 1 then it's odd. Simple.
01-11-2007, 04:25 AM#8
Feroc1ty
thanks rising +rep, ryu - any more obvious stuff to point out?
01-11-2007, 07:13 AM#9
Ryude
Sorry if I offended.
01-11-2007, 02:23 PM#10
Feroc1ty
its all good bro, sometimes you need to keep on the track and actually read the question not only the jass.