Skip to content

Latest commit

 

History

History
96 lines (68 loc) · 1.22 KB

Variable-Scope-of-Nested-Functions.md

File metadata and controls

96 lines (68 loc) · 1.22 KB
title tags created
Variable Scope of Nested Functions
bash
Julia
Notebooks/Programming
Programming
2020-07-19T02:18:32

Variable Scope of Nested Functions

Using Python and Julia can be very confusing if you are used to using R and bash.

Languages that pass variables up to parents

Functions defined in R and bash will pass variables up into there parent function, for example consider the following:

R

outer <- function() {
  inner()
  print(x)
}

inner <- function() {
  x <- 3
}

outer()
3

bash

outer() {
  inner
  echo "${x}"
}

inner() {
  x=3
}

outer
3

Languages that don't pass variables up to parents

Using Attributes

whereas in Python you would need to make the variable an attribute of the function first (I'm not sure if this feature exists in Julia?):

def outer():
    x = inner()
    print(str(inner.x))

def inner():
    inner.x = 3

outer()

Using Return

Julia

function outer()
    x=subfunction()
    print(x)
end

function subfunction()
    x=4
    return x
end

outer()
3
def outer():
    x = inner()
    print(str(x))

def inner():
    x = 3
    return x

outer()
3