I'm learning R programming. I'm unable to understand how function within function works in R. Example:

f <- function(y) {
    function()  { y }
}

f()
f(2)()

I'm not able to understand why $f() is not working and showing following message:

function()  { y }
<environment: 0x0000000015e8d470>

but when I use $f(4)() then it is showing answer as 4. Please explain your answer in brief so that I can understand it easily.

share|improve this question
    
You're defining the inner function without calling it or storing it, so it's getting returned; you've made a function that makes functions. To make the inner function get called, wrap it in parentheses, with an extra set after: f <- function(y) { (function() { y })() } though that's just equivalent to f <- function(y){y} – alistaire Dec 4 '16 at 21:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.