Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: passing a function name as an argument to another function


From   Zurab Sajaia <zsajaia@hotmail.com>
To   statalist <statalist@hsphsun2.harvard.edu>
Subject   RE: st: passing a function name as an argument to another function
Date   Wed, 24 Aug 2011 12:01:28 -0400

c
But you are explicitly referring to the name of the function - &x2() - which I'm trying to avoid. 
 
Say, I have a list of function names in a string matrix:
funcs = "func1()" \ "func2()" \ "func3()"
 
and want to go through the list and execute each:
for(i=1; i<=length(funcs); i++) {
  ...call funcs[i] somehow...
}
 

----------------------------------------
> From: kit.baum@bc.edu
> To: statalist@hsphsun2.harvard.edu
> Date: Wed, 24 Aug 2011 10:51:45 -0400
> Subject: re:RE: st: passing a function name as an argument to another function
>
> <>
> This will definitely work but it means that I'm calling stata routine from mata (my caller procedure is in mata) that does nothing other that passing arguments back to a mata function...not the most beautiful solution I guess but whatever works :)
>
>
> No need--can be done entirely within Mata. See {m-2] ftof.
>
> clear all
> mata:
> real function x2(real scalar x)
> {
> return(x^2)
> }
>
> void function test(
> pointer(real scalar function) scalar f,
> real scalar x)
> {
> xx = (*f)(x)
> xx
> }
>
> test(&x2(), 4)
>
> end
>
>
>
> Kit Baum | Boston College Economics & DIW Berlin | http://ideas.repec.org/e/pba1.html
> An Introduction to Stata Programming | http://www.stata-press.com/books/isp.html
> An Introduction to Modern Econometrics Using Stata | http://www.stata-press.com/books/imeus.html
>
>
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/ 		 	   		  
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index