| 02-26-2006, 03:00 PM | #1 |
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 JASS:call Agf(GetTriggeringUnit()) and what does a function like this do? JASS:function ret takes nothing returns integer (or should it be 0 here?) return 0 endfunction |
| 02-26-2006, 03:13 PM | #2 |
First example is correct. Second should be 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 |
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 | |
Quote:
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 |
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 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 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 call Bar(GetTriggeringUnit())? |
| 02-26-2006, 05:07 PM | #6 |
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 |
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 |
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 : 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 : 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 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 ? JASS:local unit u = GetTriggerUnit() Here u is equal to the Triggering Unit. We can simply imagine the syntax of the GetTriggerUnit() function : 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 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 |
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 |
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. |
