HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

How do you make/call functions with JASS?

11-28-2002, 02:34 AM#1
Guest
where do we put the code?
do we just leave it as part of a trigger?

how do we go about calling a function?
11-28-2002, 04:51 PM#2
SuperIKI
Just look at some maps in the trigger repository.
Dialog Engine 3.0 and many other maps use JASS.
And they all call functions.
You can call a function via the call statement.
11-29-2002, 10:35 PM#3
DaKaN
Somebody correct me if im wrong, but i 1 trigger can have multiple functions, and to make a new function:

function Title_of_Func takes nothing and returns nothing
data
endfucntion

the nothing can be replaced with varibles if it will take in a varible or returns one

to call it:
call Title_of_Func( -insert any varibles passed here- )
11-30-2002, 02:26 PM#4
Guest
do u replace nothing with a variable name or type?
11-30-2002, 04:43 PM#5
DKSlayer
Ahhhh here I can help.
Ok Lets start with a main function. Usually your main function does not return anything nore take anything so it looks say like this.
Code:
function Main takes nothing returns nothing

endfunction
That one does nothing, You could actually do everything in one trigger but that is wasteful. You usually do stuff multiple times, which you could have a function do for you and save code. Plus it is messy.

So lets say you wanted a function that would take 50 away from and integer Multiply by 9 and divide by two. You would do this.

Code:
function Math takes integer MyNumber returns integer
  set MyNumber = (((MyNumber - 50) * 9) / 2)
  return MyNumber
endfunction

There is our function let me talk about the first line of code first. 2nd Word is your function name. When you do a take you say what type it will take, ie integer, then you put the name of it right after it, like MyNumber so that you can refer to it, it's just like a variable. For return if it is a void function meaning it does not return anything you just say return nothing. If it does return something you say the return type. Make sure your return type and the data you are returning are the same type otherwise you will get an error.

So lets see these functions used together.

Code:
function Math takes integer MyNumber returns integer
  set MyNumber = (((MyNumber - 50) * 9) / 2)
  return MyNumber
endfunction

function Main takes nothing returns nothing
  call Math(125)
endfunction

There you go. Make sure you have the function your calling above the one you are calling it from. Also to do multiple arguments is easy for example.

Code:
function Math takes integer MyNumber, boolean NO returns integer
  if (No == False) then
    set MyNumber = (((MyNumber - 50) * 9) / 2)
  else
    set MyNumber = (MyNumber + 2) 
  endif
    return MyNumber
endfunction

function Main takes nothing returns nothing
  call Math(125, true)
endfunction

There you go you got the basics of making/creating a function and issueing calls to them. If you need anything else please ask.
12-01-2002, 03:27 AM#6
Guest
you have a call to a function but no where to tell where to place the returned value?

or do you use:

Set value = call Math(125, true)
etc?
12-01-2002, 03:59 AM#7
DKSlayer
I can do it one of two ways. I can store the return value
Code:
set Num = Math(150)

or I could use it in an if statement
Code:
if (Math(150) = 25) then
  --CODE HERE--
endif
There are two examples of how you can use it. You never use the word call before the function unless that function returns nothing. If it returns something you do not use call.

I messed up on that, was doing homework then. Hope that helps.
12-01-2002, 10:33 AM#8
AIAndy
You can also use the call statement when the function returns a value but you do not want to use it.