title: Accessing a variable defined in another function in Groovy
body: <p>I am new to Groovy.
  I have a function in which I am writing a value to map.</p>
  
  <pre><code>def addTraceEntry(key, value) {
      def traceability = [:]
      traceability[key] = value.trim()
      println "This print happens in function addTraceEntry " + traceability
      }
  </code></pre>
  
  <p>I have another function that needs to verify whether the above function works properly.</p>
  
  <pre><code>def testAddTraceEntry() {
      def key = 'test_key'
      def value = 'test_value'
      addTraceEntry(key, value)
      println "This print happens in function testAddTraceEntry " + traceability
      assert value == traceability[key]
  }
  </code></pre>
  
  <p>I am invoking the <code>testAddTraceEntry()</code> function using the function name:</p>
  
  <pre><code>testAddTraceEntry()
  </code></pre>
  
  <p>When I run this, I get the ERROR:</p>
  
  <pre><code>This print happens in function addTraceEntry [test_key:test_value]
  Caught: groovy.lang.MissingPropertyException: No such property: traceability for class: HelloWorld
  groovy.lang.MissingPropertyException: No such property: traceability for class: HelloWorld
      at HelloWorld.testAddTraceEntry(HelloWorld.groovy:53)
      at HelloWorld.run(HelloWorld.groovy:57)
  </code></pre>
  
  <p>In the function <code>testAddTraceEntry</code> it clearly does not know the value of <code>traceability</code> so seems like its giving an ERROR for that.</p>
  
  <p>I tried to return the value of <code>traceability</code>.</p>
  
  <pre><code>  def addTraceEntry(key, value) {
          def traceability = [:]
          traceability[key] = value.trim()
          println "This print happens in function addTraceEntry " + traceability
          return traceability
      }
  </code></pre>
  
  <p>But this yields the same ERROR.</p>