HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Constant Functions

12-15-2006, 12:58 AM#1
Joker
I see ppl use these. Whats the diff between these and variables? What are its benefits?
12-15-2006, 02:22 AM#2
Ryude
A constant is something that will never change, because of that it will process much faster.

If you know something will never change, you can keep it in the back of you mind and remember it real quick. But if you don't know if it's changed or not, you have to check real quick in the book. That takes time to look through the book, so it takes longer.

This is just an analogy.
12-15-2006, 02:27 AM#3
Anopob
Analogy = ?

From your explanation of a constant, it seems like a variable that you would need to remember all the time...huh? I don't get it.
12-15-2006, 03:04 AM#4
PipeDream
In warcraft, 'constant' is a syntactical constraint that ensures a function does not call any non-constant functions and does not use the set statement. It has no effect on the generated code. In practice we use the constant keyword to denote data such as spell configuration. When used in this manner, Vex's optimizer may be able to inline the function for improved performance.
12-15-2006, 03:38 AM#5
zen87
pipedream : I get more more confused with your explaination -.-"

well constant is just a number that remain the same, a very simple example : one day have 24 hour, so the 24 is a constant, and it never change. Variable is something changing... well in your science experiment you often in contant with some manipulated variable and responding variable... arrr this is getting longer...

anyway for an example, lets say a line equation, y=mx+c, so y is the responding variable, m and c are more or less the constant (they dont change no matter what x you put in) and x is the manipulated variable. Understand this and you will one step closer to understanding functions.
12-15-2006, 03:54 AM#6
Ammorth
It looks like this topic strayed off-course, so I'm going to answer the posters question to the best of my abilities.

Joker, a constant function is used in place of a variable, because:
1) It's easy to change.
2) All your constants for one "idea" (spell, system, etc.) are in a single area, instead of spread-out along the variable manager.
3) It requires no global variable to create, which makes the "idea" easier to copy from map to map. Instead of having a list of "variables required and their default properties" the user just copies the entire "idea" and doesn't have to worry about all of that.

Hope this answers your question.
12-15-2006, 06:20 AM#7
Captain Griffen
Constants functions == functions with restraints.

Only really useful with Vex's optimiser.
12-15-2006, 10:47 PM#8
Joker
Quote:
Originally Posted by Captain Griffen
Constants functions == functions with restraints.

Only really useful with Vex's optimiser.
why and whats does vex's optim do?
12-15-2006, 10:52 PM#9
Captain Griffen
Read Pipe's post. Rule of thumb: Pipe is Always Right.
12-15-2006, 10:54 PM#10
wyrmlord
Vex's optimizer opens up your map or a script and decreases the size of it by using many different methods, and most of those methods deal with decreasing the size of the map's script. Since constant functions are limited in what they can do, the optimizer can inline them or something to improve performance. Basically, a constant function is a function that cannot call non-constant functions or set variables (from my understanding).
12-16-2006, 03:07 AM#11
Vexorian
Quote:
Originally Posted by Captain Griffen
Read Pipe's post. Rule of thumb: Pipe is Always Right.
Hey, I reclaim the authorship of that rule.

Eventually, the optimizer will get 'smarter' and not require the constant keyword to inline, either way I think it is still great as a way to document code. A constant function or a constant 'variable' make sense to differentiate configuration from actual code. Although it does increase the chances of WE's compiler crashing when saving but it is not like a human being would actually use that compiler...
12-16-2006, 03:33 AM#12
Joker
so am i able to set a constant function for anything? like triggers, units, reals, etc
12-16-2006, 03:59 AM#13
Vexorian
that question is confusing
12-16-2006, 02:02 PM#14
BertTheJasser
I guess some examples will help:
Collapse JASS:
constnat function fx takes real x,real m,real c returns real
returns m*x+c
endfunction
OK

Collapse JASS:
constant function string s returns integer
returns OrderId(s) //OrderId is a constant function aswell
endfunction
OK

Collapse JASS:
constant function string s returns string
local string k=s
returns s
endfunction
ERROR

Collapse JASS:
constant function integer i returns integer
set i=i*2
returns i
endfunction
ERROR

Collapse JASS:
constant function real r,real q returns real
returns Pow(r,q)
endfunction
ERROR
12-17-2006, 02:31 PM#15
Ryude
Quote:
Although a constant value is specified only once, the constant variable can be referenced multiple times in a program. Using a constant instead of specifying a value multiple times in the program can simplify code maintenance, not only to simplify changing its value but also to supply a meaningful name for it and to consolidate such constant assignments to a standard code location, for example at the beginning.

Quoted from Wikipedia.