"Dynamic" Functions

T.s.e

Wish I was old and a little sentimental
Reaction score
133
Say, if I wanted to get the average of a number of real values.

JASS:
function GetAverage takes real uno, real dos, real tres, real cuarto, real cinco, ... integer num returns real
return uno+dos+tres+catorce+cinco/num
endfunction


So, if the numbers were different, and there were 6 factors to include instead of the 5, would I have to create a whole new function? Or is there a way to make taken values "dynamic"?
Thanks in advance.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
The average of a, b and c is not a + b + c / 3 but (a + b + c) / 3.

Other than that, if your function expects 20 parameters... well, it won't work with any less.


Though, it is possible to only consider the first "num" numbers.
But you must still pass them when calling the function.

Some huge if then elseif then elseif then else... :p
 

Gwypaas

hook DoNothing MakeGUIUsersCrash
Reaction score
50
Maybe a struct that contains all the values and the max amount and then use a loop to do your task.
 

Vexorian

Why no custom sig?
Reaction score
187
Nope, no dynamic arguments for you!

Try an array.

How to pass an array though?

Perhaps you need a dynamic array, but then I think a struct solver object would be best?

JASS:
struct averageSolver
    integer array args[20]
    integer  n =0

    method solve takes nothing returns real
      local integer i=0
      local real sum=0
        loop
             exitwhen i==this.n
             set sum=sum+this.args<i>
             set i=i+1
        endloop
     return(sum/this.n)
    endmethod
    


endstruct 

function crazyStuff takes nothing returns nothing
 local averageSolver s=averageSolver.create()
    set s[0]=1
    set s[1]=2
    set s[2]=3
    set s[3]=4
    set s[4]=5
    set s[5]=6
    set s.n=7
    call BJDebugMsg(R2S(  s.solve() ) )
    call s.destroy()
endfunction

</i>
 

Forty

New Member
Reaction score
6
Nope, no dynamic arguments for you!

Try an array.

How to pass an array though?

Perhaps you need a dynamic array, but then I think a struct solver object would be best?

JASS:
struct averageSolver
    integer array args[20]
    integer  n =0

    method solve takes nothing returns real
      local integer i=0
      local real sum=0
        loop
             exitwhen i==this.n
             set sum=sum+this.args<i>
             set i=i+1
        endloop
     return(sum/this.n)
    endmethod
    


endstruct 

function crazyStuff takes nothing returns nothing
 local averageSolver s=averageSolver.create()
    set s[0]=1
    set s[1]=2
    set s[2]=3
    set s[3]=4
    set s[4]=5
    set s[5]=6
    set s.n=7
    call BJDebugMsg(R2S(  s.solve() ) )
    call s.destroy()
endfunction

</i>


wouldnt it be s.args[...]?
oh and write average with uppercase :p
 

Vexorian

Why no custom sig?
Reaction score
187
yep.

types in normal Jass are lower case and non camelized, I guess that means I would have to convert averageSolver to averagesolver

Yes. it is s.args , alternatively:

JASS:
struct averageSolver
    delegate integer array args[20]
    integer  n =0

    method solve takes nothing returns real
      local integer i=0
      local real sum=0
        loop
             exitwhen i==this.n
             set sum=sum+this.args<i>
             set i=i+1
        endloop
     return(sum/this.n)
    endmethod
    


endstruct 

function crazyStuff takes nothing returns nothing
 local averageSolver s=averageSolver.create()
    set s[0]=1
    set s[1]=2
    set s[2]=3
    set s[3]=4
    set s[4]=5
    set s[5]=6
    set s.n=7
    call BJDebugMsg(R2S(  s.solve() ) )
    call s.destroy()
endfunction

</i>

This one should compile.
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
I see. Silly of blizzard not to include something like this. Anyway, thanks for the responses, +repped the helping people.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top