HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

takes ***** returns ***** = ?

02-26-2006, 03:00 PM#1
Thunder_Eye
Collapse JASS:
function Agf takes unit u returns nothing
   call KillUnit(u)
endfunction

Ive never understood these "takes bla returns bla" functions.
Where do it actually set the u unit? is it when you call the function like
Collapse JASS:
call Agf(GetTriggeringUnit())
or how else is it done?

and what does a function like this do?
Collapse JASS:
function ret takes nothing returns integer (or should it be 0 here?)
   return 0
endfunction
02-26-2006, 03:13 PM#2
Blade.dk
First example is correct.

Second should be

Collapse JASS:
function ret takes nothing returns integer
   return 0
endfunction

as you can't always know the value.

btw http://jass.sourceforge.net/ has a great manual about jass, I recommend that one to everybody.
02-26-2006, 03:17 PM#3
Thunder_Eye
ok nice, is a function with "takes blabla" faster then one that uses locals instead?

And do I have to clean up the ones in the "takes ...."?
02-26-2006, 03:22 PM#4
PitzerMike
Quote:
Originally Posted by Thunder_Eye
ok nice, is a function with "takes blabla" faster then one that uses locals instead?

You can't use normal locals to pass parameters to a function. Well, in fact the parameters ARE locals, only that you have to pass their initial value whenever you call the function.
The point of it is re-usability of functions.
02-26-2006, 03:27 PM#5
Thunder_Eye
Well before I used a method where I saved the data on a global and then in the function I set a local with that value.

Do I always have to set all values when I call it?
So I have to do it like
Code:
call ABC(1,0)
function ABC takes integer a, integer b returns nothing
   set b=a+2
endfunction
or can I somehow just
Code:
call ABC(1)
function ABC takes integer a, integer b returns nothing
   set b=a+2
endfunction

And to the other question how does the "return" thing work? (I'm reading the posted manual)

EDIT:Just read some in the manual there's a function there like this
Collapse JASS:
function Bar takes unit u returns nothing
     call PauseUnit(u, true)
     // the unit u is paused, but u and y refer to the same
     // data structure so y is also paused
endfunction

...
     local unit y = GetTriggeringUnit()

     call Bar(y)
     // the unit y is paused

so do I have to have an variable within the parameters or could it aswell be
call Bar(GetTriggeringUnit())?
02-26-2006, 05:07 PM#6
Anitarf
When calling a function, you must always give it all the parameters it requires. However, not all local variables a function uses need to be parameters, as you can declare locals independantly. Parameters need only be used for information that the function needs to receive from the outside to work; you can also pass the information to it with golbals, but that's just less practical.

In the function example you gave, only a needs to be a parameter, while b can just be a local, since it's only used for internal function computations.

The return value of a function is a way for functions to pass information on to the function that called them. A function that returns a value can be used anywhere where that value could be used. For example, GetTriggerUnit() returns a unit and can be used anywhere where oyu would otherwise use a unit variable.
02-26-2006, 06:34 PM#7
Thunder_Eye
ok thank you, though I'm still unsure on the "return"

call ABC(1)
function ABC takes integer a returns nothing
local integer b
set b=a+2
endfunction
02-26-2006, 08:38 PM#8
RaptorTeak
Returns is there to transfer some data to the call when it finishes. For example, it can returns something like "TRUE" or "FALSE", then used by a trigger or another function.

I'll make a basic example with your last function with the return utility :
Collapse JASS:
function ABC takes integer a returns integer // you have to specifie the type of the return
local integer b
set b=a+2      // You can Also write on one line "local integer b = a+2"
return b       // there we order this function to return to the call the value of the variable b
endfunction

then, in another function, imagine you got that :
Collapse JASS:
function blabla takes nothing returns nothing
local integer i = ABC(2) // We order the function ABC, with the parameter "2" (a will be set as "2")
endfunction

Then, ABC will calcul 2+2 (because we set the value into () to "2"), set this calcul to b, and return b. In the "blabla" function, the variable b will be returned, and then "i" will be equal to 4 :).

if I made
Collapse JASS:
local integer i = ABC(8)

Then i would have been equal to 10. If I didn't put any return to the ABC function, then "i" would have been equel to "null" or "0".

As said Anitarf, GetTriggerUnit() is a function that returns a unit, isn't it ?

Collapse JASS:
local unit u = GetTriggerUnit()

Here u is equal to the Triggering Unit. We can simply imagine the syntax of the GetTriggerUnit() function :

Collapse JASS:
function GetTriggerUnit takes nothing returns unit // takes nothing because we don't have to specifie anything into the ()
local unit u
// Somethings to find the triggering unit and to set it as the variable "u"
return u
endfunction
local variables are only working in functions. Meaning that you could name the "i" variable in the "blabla" function to "b" and it would have changed nothing, because local variables are ranged only in their functions.

Only globals variables (the who begin with "udg_") are taking effect everywhere, that's why you can't define local variables to "udg_xxxx".

I hope I've been clear, and sorry for my english :).
02-26-2006, 08:45 PM#9
Thunder_Eye
Oh, so a return function is like a.. calculation?
The main function just uses it to calculate something which it then uses.
Kind of how we use an calculator. (Though those cant use strings and handles;P)
Though in this example wouldnt it be faster if "blabla" would just calculate 2+2 as it then just uses one function?
(Ive just begun with JASS, Ive worked in GUI since RoC got released, so its mostly these kind of things in JASS that I dont get)

That made it much clearer thank you
02-26-2006, 08:54 PM#10
RaptorTeak
Yeah that's it.

But imagine when you'll have a BIG project, needing to make the same complex calcul multiple times, with different factors. Just make a function taking your needed variables, make the complex calcul, and return the result. It's better, because you'll have only one time your complex calcul written instead of writting it each time you need it.