Your error is because you are passing a variable that isn't defined either in the Main
method or globally. You should change the line to square(a)
to pass in the array you defined just before it.
Secondly, your square
method has a return type of double[]
, but you aren't assigning it to anything. If all you want to do is show the squared values, you could simply make it a method of return type void
. On the other hand, if you do want to actually use the array returned by this method, then you will need to declare another variable and assign to it like so:
double[] b = square(a);
The main advantage is that, if you need to use the array returned by this method, say, 10 times later in your code, you can just use the variable instead of calling the method 10 times.