Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[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 <[email protected]>
To   statalist <[email protected]>
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: [email protected]
> To: [email protected]
> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index