Constant Functions

Discussion in 'JASS Zone' started by Ayanami, Jun 16, 2011.

  1. Ayanami

    Ayanami 칼리

    Ratings:
    +287 / 0 / -0
    So I've been pondering about constant functions. I heard that constant functions always return a constant return value. However, this seems to work.

    JASS:
    
    private constant function GetDamage takes integer level returns real
        return level * 5.
    endfunction


    By right, the above function can't be a constant, right?
     
  2. Solmyr

    Solmyr Active Member

    Ratings:
    +30 / 0 / -0
    A constant function is a function that does not call any other non-constant functions and does not use the set statement. Following this, the function you posted is indeed constant.

    In practice, constant functions are used (as your example shows) to denote data such as spell configuration. In terms of execution speed, it is absolutely the same as a non-constant function, however, Vexorian's map optimizer will always try to inline constant functions, which would ultimately result in a minor speed gain (note that JassHelper can also inline stuff, but the optimizer's inlining is much more "powerful"). There's also a consant function inliner by blu_da_noob (I haven't tested that one recently, but last time I did, it worked fine).
     
  3. emjlr3

    emjlr3 Change can be a good thing Staff Member

    Ratings:
    +396 / 0 / -0
    really the idea is, you allow users easy customization of spells/systems using the constant function - but then enable the speed efficiency of in-lining during processing
     

Share This Page