title: How to create a summation function and call it, by passing Int array in swift playground?
body: <p>I have created a function that performs summation of numbers, by accepting Int array and returning the sum of elements as integer value. Below is my code, which I checked to be logically correct, but I am getting error during runtime mentioning,'index out of range' in console and "</p>
  
  <blockquote>
    <p>error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>import UIKit
  var x=[Int]()
  var i=0
  func dosomething(y:[Int])-&gt;Int{
  
      for index in 0..&lt;y.count{
          if index==0{
              x[index]=0
              x[index]=x[index]+y[index]
              i=x[index]
              }
          else{
              x[index]=x[index-1]+y[index]
             i=x[index]
          }
         }
      return i
  }
  
  dosomething(y:[1,2,3])
  </code></pre>