Q: How to define the access scope of a nested function as global? I have an R code which looks like:
func1 = function(a) {

  func2 = function(a) {
    return(a+2)
  }

  func3 = function(a) {
    return(a+3)
  }
  return(a+func2(a))
}

Is it possible that I be able to call func2 or func3 from outside func1?
eg. How do I run:
x <- func2(10) #from the console?


A: You could create a function closure:
##I've removed the brackets and return to shorten the function
func1 = function(a) {
  func2 = function(a) a+2
  func3 = function(a) a+3
  return(list(func2=func2, func3=func3))
}

You can use the closure to share variables:
func1 = function(a) {
    a = a
    func2 = function() a + 2
    func3 = function() a + 3 
    return(list(func2=func2, func3=func3))
}

f = func1(50)
f$func2()
f$func3()


A: No You can't.
func2 is defined in the scope of func1.
But The question is really ambiguous ! Any netsed function has access to the global scope.
a <- 10
function <- f(x){
   g <- function(y=x) x+a
   g(x)
}

here function g is nested and has a free variable a. 
 the interpreter lokks in the scope of g and f and next looks for a value for a in the local frame of f(the global)
Why defining a nested function as nested if you want access to it as global?
