Hi everyone, I have two questions regarding passing a function name as an argument.<div><br></div><div>(1). Suppose that I have a function foo() shown below. The function takes a NumericVector and a function pointer that points to a function that takes a NumericVector and returns a double. Note that the function pointer will only point to functions not exposed to R.</div>

<div><br></div><div><div><font face="courier new, monospace">double foo(NumericVector x, double (*f)(NumericVector x) ){</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">   </span>double output;</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>output=(*f)(x);</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">     </span>return output;</font></div>

<div><font face="courier new, monospace">}</font></div><div><br></div></div><div>When I try to compile it as is using sourceCpp(), it's fine, but when I add "// [[Rcpp::export]]" above the function definition, I get an error message:</div>

<div><br></div><div><div><font face="courier new, monospace">Error in sourceCpp("foo.cpp") : </font></div><div><font face="courier new, monospace">  Error 1 occurred building shared library.</font></div><div><font face="courier new, monospace">foo.cpp:229: error: expected initializer before ‘SEXP’</font></div>

</div><div><br></div><div>So I wonder how I can fix this mistake.</div><div><br></div><div><br></div><div>(2). Imagine a more complex scenario: suppose there are two functions available to be passed to foo(). But the two functions differ in the number of arguments each has (see fun1() and fun2() below). I wonder if there is any way to deal with this.</div>

<div><br></div><div><div><font face="courier new, monospace">double fun1(NumericVector x){</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre"> </span>double total=0;</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>for(int i=0;i<x.size();i++)</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">              </span>total+=x(i);</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>return total/10;</font></div><div><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br>

</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">double fun2(NumericVector x, int n){</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">   </span>double total=0;</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>for(int i=0;i<x.size();i++)</font></div><div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">              </span>total+=x(i)+add;</font></div>

<div><font face="courier new, monospace"><span class="Apple-tab-span" style="white-space:pre">  </span>return total/n;</font></div><div><font face="courier new, monospace">}<span class="Apple-tab-span" style="white-space:pre">    </span></font></div>

<div><span class="Apple-tab-span" style="white-space:pre"><font face="courier new, monospace">  </font></span></div></div><div>Thank you in advance!</div><div><br></div><div><br></div><div>Best,</div><div>-Xiao</div><div>
<br>
</div>