Impossible to answer without seeing the code for the functionsum( ) function. Maybe you could post that? Maybe all you need to do is this (but this is just a guess w/o seeing your code):
What version of MATLAB are you using?
Btw, you don't need to explicitly cast the i and j arguments since C has automatic type promotion. This would suffice:
plhs= mxCreateDoubleMatrix( i, j, mxREAL);