New to JASS: What is takes and return for?

C

Cs_Cara

Guest
I read lots of tutorials and I cant find out what takes and return are for...

Code:
function Trig_Init [b]takes[/b] nothing [b]returns[/b] nothing
What these things do, with or without a value? *getting crazy*
 

Luth

Lex Luthor!
Reaction score
41
Functions are bits of code made up of a few different parts:
Function Name: What you call to invoke the function
Parameters: Values that you pass into the function to give it knowledge of the things you want to modify
Return Value: A value returned by the function to whoever called it
Function Body: The actual code that is run

function NAME takes PARAMETERS returns RETURN VALUE
BODY
endfunction

For example:
function GetHitpoints takes unit MyUnit returns real
return GetUnitProperty(Life, MyUnit)
endfunction

The function must know about the unit in question in order to query its current HP, and the code that calls the function expects a real number (the nuber of Hit Points) to be returned, akin to: real HP = GetHitpoints(someUnit)
 

SFilip

Gone but not forgotten
Reaction score
634
generally in programming every function has to take one or more parameters and then return a value.
for example
Code:
function BJDebugMsg [B]takes string msg[/B] returns nothing
    local integer i = 0
    loop
        call DisplayTimedTextToPlayer(Player(i),0,0,60,msg)
        set i = i + 1
        exitwhen i == bj_MAX_PLAYERS
    endloop
endfunction
it takes a variable of type string and it refers to it using "msg".
so when you call it, you must give it a string.
call BJDebugMsg("example")
the function itself gets "example" by using msg the same way you would use a local.
the functions that take nothing can simply be called with call Function()

as for returns...
Code:
function Two takes nothing [B]returns integer[/B]
    return 2
endfunction
so now you can use this function like this
local integer i = Two()
or simply
set i = Two()
this would always result in i being 2 because the function Two returns an integer - 2.
this function can also be called with call Two() but it will have no effect in this case.
also as you may guess you can both take and return
Code:
function AddTwo takes integer int returns integer
    return int + 2
endfunction
local integer i = AddTwo(5) will now set i to 7.
for more examples take a look at blizzard.j.
 

Luth

Lex Luthor!
Reaction score
41
Well, consider you wanted a function to add two numbers together. Call it "Add." So you've got:
Code:
function Add
It has to know a few things, right? It has to know what numbers you want to add together. Call 'em "integer A" and "integer B". So now we have:
Code:
function Add takes integer A, integer B
But what good is calling that function going to do if we dont know the result? So we finish it off with "returns integer", which identifies the type of the result returned by the function. The final product is:
Code:
function Add takes integer A, integer B returns integer
Great, now we know all about the function... but it doesnt do anything yet. So add the body, "return A + B". This will add A to B, and return the results [note: return ends the function immidiately, and passes that value back to the calling function] Our final function is:
Code:
function Add takes integer A, integer B returns integer
    return A + B
endfunction
and we would call it like:
Code:
local integer SUM
set SUM = Add(5, 7)
And, as expected, SUM is now equal to 12.
 
C

Cs_Cara

Guest
SFilip said:
Code:
function BJDebugMsg [B]takes string msg[/B] returns nothing
    local integer i = 0
    loop
        call DisplayTimedTextToPlayer(Player(i),0,0,60,msg)
        set i = i + 1
        exitwhen i == bj_MAX_PLAYERS
    endloop
endfunction
it takes a variable of type string and it refers to it using "msg".
so when you call it, you must give it a string.
call BJDebugMsg("example")
the function itself gets "example" by using msg the same way you would use a local.
But how it will know that msg is equal to "example"?
 

SFilip

Gone but not forgotten
Reaction score
634
when you call it using
call BJDebugMsg("example")
you actually put the value you want msg to take between ( ).
the function simply refers to whatever you put there as msg.
 
D

dArKzEr0

Guest
Parameters can be locals from other bits of code, but they can really be anything.

A function is created with the intention of using it, or "calling" it somewhere else in code.

- - call functionABC(x)

Let's assume the purpose of functionABC is to return the letter of the alphabet that corresponds to the integer you "pass" as a parameter.

If you write "call functionABC(5)"

5 is your parameter. The return value would of course be "e".

Some functions don't even require parameters:

Code:
function HelloWorld takes [B][U]nothing[/U][/B] returns nothing
    call DisplayTextToForce( bj_FORCE_ALL_PLAYERS, "Hello World" )
endfunction

This function will display to all players the text "Hello World" every time it is called:

call HelloWorld()

Notice there are no parameters between the parenthesis. That's because the function takes "nothing".

When a function takes nothing, it tends to be more constant than a function which requires parameters, usually producing the same result each time it is called.

call HelloWorld()
call HelloWorld()
call HelloWorld()
call HelloWorld()

That will display "Hello World" to every player four times.

If you have a trigger with this:

call DisplayTextToForce( bj_FORCE_ALL_PLAYERS, functionABC(1) )
call DisplayTextToForce( bj_FORCE_ALL_PLAYERS, functionABC(2) )
call DisplayTextToForce( bj_FORCE_ALL_PLAYERS, functionABC(3) )
call DisplayTextToForce( bj_FORCE_ALL_PLAYERS, functionABC(4) )

Then the letter "a" will be displayed, then the letter "b", then the letter "c", and finally the letter "d". The parameters determine the result of calling the function.

Hopefully this was clearer for you.

-darkz
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top