• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Introduction to Functions [WIP]

Status
Not open for further replies.
Level 20
Joined
Apr 22, 2007
Messages
1,960
draft: Introduction to the concept of a function
Edition 2

The concept of a function is often the most confusing thing for a beginning Jasser to understand, if you've never seen a scripting or programming language before. Once understood, though, the rest of the language comes very easy.

How do I write in Jass?
First things first, if you don't know how to write in Jass: In the trigger editor, create a new trigger. Select it an click on "Edit > Convert to custom text". Delete the text inside the trigger. In that space, you can now write any Jass you want.
You can also access the map's custom script header. In the trigger editor, where you normally see the trigger list, click on the map icon. An empty white box opens up where a trigger's body usually is. You can write your Jass in there.
If you feel like using a third party Jass editor, I'd suggest JassCraft.
I highly suggest you download and use Jass NewGen Pack for the duration of these lessons. It will save you from content-losing crashes, and it also comes equipped with a pretty Jass syntax highlighter.

Comments, for the purpose of clarity:
JASS:
// Double-slashes indicate comments
// Anything to the right of a double slash will not be compiled when you save your map.
// These are called comments.

What is a function?
Functions are very self-explanatory. They accomplish tasks that are either defined by you or by Warcraft III's inner coding. Functions are 'called' in a script. You call a function, and it accomplishes whichever task you defined it to do.

The Jass syntax for declaring functions goes as follows:
JASS:
function name takes nothing returns nothing
    // The function's body goes here.
endfunction
The beginning and the end of their definition is function and endfunction, respectively. You would replace 'name' with whichever name you want to give to a function.

But what good is a function if it has nothing to work upon? For example, in mathematics, functions are given numbers and apply specific operations to them, then return a result. Imagine following mathematical function:
f(x) = x2
If you want to calculate f(2), then you would do:
f(2) = (2)2 = 2*2 = 4
The function f(x) was given the value 2, and it returned the value 4. This is the same principle as a function in Jass, but Jass functions can accomplish much much more than simply squaring a number.

The mathematical function given above has a number parameter: we can give a number to the function, and it will return a result. We cannot calculate f("hello"), since "hello" is not a number. Thus the concept of a data type is introduced.

~ to be continued ~
 
Last edited:
Status
Not open for further replies.
Top