Return Error with double return value in func

Asked 2 years ago, Updated 2 years ago, 103 views

When I set the return value to double type in func, I get an error in return.
If anyone knows the cause, please let me know.

 varpcascore: Array <Double > = [3.2, 2.4, 3.5, 1.3]

funcarrayplus(pcascore:Double...) - > Find the total value of the Double {// argument
    variable —Double = 0.0

    for n in pcascore {
        total+=n
    }
    // print (total)
    pcaallscore.append(total)

    return pcaallscore
    // Cannot convert return expression of type '[Double]' to return type 'Double'
    // The error appears.
}

swift xcode

2022-09-30 21:10

2 Answers

I think pcaallscore is a double list, and it's because it's a double (single variable) that should be returned.
If you want to return an array, define the function.

function.
 funcarrayplus (pcascore: Double...) - > Array <Double > {

Shouldn't it be?


2022-09-30 21:10

You don't need pcaalscore.append(total) to just return the sum. I think return total is fine.


2022-09-30 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.