• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[JASS] Functions, Globals, "takes" and "returns"

Status
Not open for further replies.
I recently learned the basic of JASS (variables, functions, globals, loops, etc), by there are a few things that I just can't figure out...

Now, I know the difference between Bilzzards functions, and user functions, but I cannot find a damned thing on how to use them.
Could someone please tell me where to put them (in a trigger converted to jass, or custom map script) so I can use them, and go I to the most possible detail about EVERYTHING there is to know about functions? (blizz and user)

Also, how exactly do globals work? I know locals, but I have no idea how globals work.


And lastly, can some explain what "takes" and "returns" does?

Thanks for any help,
TheLifelessOne
(ps: I have NewGen and JASSCraft)
 
Level 12
Joined
Jul 27, 2008
Messages
1,181
Takes, is a parameter. You know what locals are right? Well, if I have this:

JASS:
//Takes is a parameter to pass locals, as I now pass u from B to A
function A takes unit u returns unit
    //.........actions
endfunction
//Returns is used to set variables, you can use a function to set a 
//variable that is it's return type.
function B takes nothing returns nothing
    local unit u = ......
    local unit u2 = A(u)
endfunction

Put functions anywhere, as long as they're in themap & above the function that calls them.

Globals are GUI variables, they can be accessed from every function, and have only one instance, unlike locals.
 
Level 12
Joined
Jul 27, 2008
Messages
1,181
Set it to a local (or use it as a parameter for other functions/natives)
eg.
JASS:
//R2I is real to integer.
//GetRandomReal() is a native that returns real, and 
//because R2I takes real I can use that instead.
call R2I(GetRandomReal())

"takes" is useful for transferring locals, if you want a function to use a variable, but that variable is a local then you call it like:
JASS:
call MyFunction(MyLocal)

EDIT: Just saw smth, if you're new to jass ditch NewGen for now.
 
Yeah, you pulled out that "R2I" thing, and confused the hell out of me.
As to takes, I could do something like this (remember, I just learned JASS about 2 days ago):

JASS:
function myFunct takes nothing returns nothing
    local integer int = 5
endfunction

function myFunct2 takes 5 turns nothing
    call MyFunct(5)
endfunction
And that'll take "5" from "myFunct", and allow it to be used in "myFunct2", right?
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
No, first, a function can only 'take' types, that means integer, boolean, real, unit, leaderboard, whatever, but a function cannot 'take' a value, be it 5, true, 6.789, gg_unit_hpea_000 (a rawcode for preplaced units).

Now back to the function above.
function myFunct takes nothing, that means, when you call it, you set nothing between the 'takes tags'. The 'takes tags' are the () behind a function.

Now you want to use the '5' from myFunct in myFunct2. You must return the 5 value then. 'return' means 'give it to the other function'. Let me explain:
JASS:
function F1 takes nothing returns integer
    return 5
endfunction
 
function F2 takes nothing returns nothing
    // now we want the '5' to get in this function.
    // AND we want to use it. Now what is the use of
    // calling it if you dont save it in a (local) variable?
    local integer i = F1()
    // F1 takes nothing, so there is nothing between the ().
    // F1 returns an integer, so we create an integer variable 
    // and save what F1 returns in it.
    // Now we can use it.
    local string s = I2S(i)
    // I2S converts an integer to a string. I2S 'takes' an integer (we put i here)
    // and I2S 'returns' a string, which we save in a local.
    call BJDebugMsg(s)
    // call BJDebugMsg(string) just says a message in the game. not important here.
endfunction
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
Oh, one thing.

When a function 'takes' a type (e.g. integer), it needs a name, but a 'return' is only a type, so:
JASS:
function F1 takes integer returns nothing // WRONG!
function F2 takes nothing returns integer i // WRONG!
function F3 takes integer returns integer i // DOUBLE WRONG!
function F4 takes integer i, integer j returns integer // totally fine

A function can also take serveral parameters, like the last one above. Then you put a comma (,) in between.
 
Yeah, it's one of the first things I got, when I decided to learn JASS.
Thanks a lot guys. I'll rep you asap (if it'll let me).

Just one last question, and I'll stop bugging you :)P):
For some unknown reason, whenever I put all this into NewGen, and try to test it, nothing happens, but JASSCraft successfully parses it. Could you tell me what I'm doing wrong?
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
This script, by the way, can be optimized.
JASS:
function F1 takes nothing returns integer
    return 5
endfunction
 
function F2 takes nothing returns nothing
    local integer i = F1()
    local string s1 = I2S(i)
    // When you call a function, and you need its return value,
    // AND you need it only once, you can put it immediately in 
    // the new function. Like this:
    local string s2 = I2S(i)
    // Now again, we can inline this (make shorter), because we
    // only need the string s once, we can put it immediately in the
    // BJDebugMsg(). Like this:
    call BJDebugMsg(I2S(F1()))
    // does the exact same thing as this: (in one line only!)
    call BJDebugMsg(s1)
endfunction

This is called inlining.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Think of your math classes in school.

Say you have the line quotation y = 3x + 5.
Usually (at least here) you will actually write it as y(x) = 3x + 5.
Why? Because you give that quotation a value, which is x, and that quotation returns the correct y value at that x.
In Jass this will look like this
JASS:
function y takes real x returns real
    return 3 * x + 5
endfunction
Now, exactly like in math you will tell it to work (call it) by typing y(3) or any other value.

Ever used any trigonometry functions? (look, they are even called functions)
Each one of them takes a value, and according to that value returns to you the answer.

That's how take and return works in programming.
The only difference is that you can also take or return nothing, which at the start may seem useless, but as you'll get better you'll see its uses.
 
Status
Not open for further replies.
Top