6
I would like to know how to put as an argument of a function another function.
For example, let’s say I want to solve an integral by a given numerical method of approximation of an integral. And I want to create a function that does that, where the arguments of the function are:
Integral = function(a,b, fx)
In which a and b are the integration intervals and fx is the role I want to integrate.
It’s not working yet. See:
trapezio.integracao <- function(a, b, fn){ fn(x) }
 {
   x = seq(a, b, by = 0.005)
   n = length(x)
   integral = 0.5*sum((x[2:n] - x[1:(n-1)]) * (f(x[2:n]) + f(x[1:(n-1)]) ) )
   return(integral)
}
fx <- function(x){x^2}
trapezio.integracao(0,1,fx)
If I understand what you are trying to do, you are making two mistakes there: 1 -
trapezio.integracao <- function(a, b, fn){ fn(x) }this defines a function that applies the functionfnto an objectxthat does not exist within the scope of that function 2 - the excerpt below that is what you probably want to use within the function and yet, you are not making use of the argumentfn. See my updated response to better understand.– Jon Cardoso