Function not returning values if in a separate file

Func scalaTrafila(num1:Double,num2:Double,num3:Double,num4:Double,num5:Double,num6:Double,num7:Double,num8:Double){


var scalaRiduz:[Double] = []


……(the Array is populated by some calculations)


}


This Function is working well and return the Array scalaRiduz[] if inserted into the main program (ViewController.swift). If I put this Function in a separate file it works and calculate the results at his internal, but is not returning to the main program the populated Array. Someone can understand this?

Where do you declare var scalaRiduz:[Double] = [] ?


You should have a visibility scope problem …

You are misunderstanding "return". The function 'scalaTrafila' does not return anything. If you want to return an array, you'd do something like this:


func scalaTrafila(/* your parameters */) -> [Double]
{
var scalaRiduz:[Double] = []
……(the Array is populated by some calculations)
return scalaRiduz
}


Note the return type specified at the end of line 1, and the "return" statement in line 5.

Ty for your replay. If I print the scalaRiduz before the return I can see printed all the elements. When I print scalaRiduz after the return the array is empty. What I do not understand is if I copy this function into the main program every thing is working. When I put the function in a separate file (together with all the other functions) it is not returning any value (only this function).

Ty for your replay- I declared scalaRiduz after the function declaration. What is the visibility scope problem?

>> If I print the scalaRiduz before the return I can see printed all the elements. When I print scalaRiduz after the return the array is empty.


Translating this into code, slightly simplified:


func scalaTrafila ()
{
     var scalaRiduz:[Double] = [0, 1, 2]
     print (scalaRiduz) // OK
}

print (scalaRiduz) // <-- FAILS TO COMPILE, with error "use of unresolved identifier 'scalaRiduz'"


The problem is that you're not showing us the code in your "main program".

Main Program

var scalaRiduz:Double = 0

....

....

scalaTrafila(outletDiamD,num2:inletDiamD,num3:lastReduction,num4:skinpass,num5:diesNumberD,num6:annealedDiam,num7:carbonContent,num8:speedLastDie)

print(scalaRiduz[0]) //fatal error: Index out of range


Function on a separate file

func scalaTrafila(num1:Double,num2:Double,num3:Double,num4:Double,num5:Double,num6:Double,num7:Double,num8:Double)

{

var scalaRiduz:[Double] = [0, 1, 2]

print (scalaRiduz) // OK

}



If a copy and paste this function into the main program, every thing is working well.

There are two problems here.


1. You have 2 variables called 'scalaRiduz'. They may look the same, but they are completely different. You could have written:


var scalaRiduz1111:Double = 0
…
func scalaTrafila (…)
{
var scalaRiduz2222:[Double] = [0, 1, 2]
…
}


Why? Because a 'var' statement always declares a new variable. In your existing code, the 'scalaRiduz' inside the function hides the 'scalaRiduz' outside the function. But they're still different variables.


2. This isn't your real code. In your "main program", your 'scalaRiduz' isn't even array — it has type "Double", not "[Double]".


Here's what I think happened. You had code like this, in a single file:


var scalaRiduz:[Double] = []

func scalaTrafila (…)
{
     scalaRiduz:[Double] = [0, 1, 2] // <-- Note that there is NO "var"
}

print (scalaRiduz[0]) // prints "0"


Then you split it into 2 files:


var scalaRiduz:[Double] = [0]
scalaTrafila ()
print (scalaRiduz[0])


func scalaTrafila ()
{
     scalaRiduz:[Double] = [0, 1, 2] // <-- Error: 'scalaRiduz' is not defined
}


and then "fixed" the function like this:


func scalaTrafila ()
{
     var scalaRiduz:[Double] = [0, 1, 2]
}


Now your main code will give you the error "Index out of range". Right?

Yes, fixing the declaration I did


"var scalaRiduz:Double = 0" TO "var scalaRiduz:Double = []"


and running the program with two split files I had the "Index out of range"

So, what is (now) your question? Your code is doing what you told it to do.


Yes, you are rigth. If the function is working inside the main program it is working well. If I put the function into a split file I receive the message: "fatal error: index out of range". I cannot understand it, but it is not a serious problem. I want to thank you very much for your assistance.

Function not returning values if in a separate file
 
 
Q