program error returning power of value in swift

Asked 2 years ago, Updated 2 years ago, 34 views

A program error returns a power of values in swift2.0.What is the cause?
Professor, please.

// Added //

import UIKit
import Foundation

classpcscoredistance {

    // A program that determines the power of a number
    funcruizyou(lhs:Double, rhs:Double) - > Double {

        letruizyou1 = power(lhs, rhs)
        return ruizyou1
    }
    varn = ruizyou (-9.0, rhs:2.0) // Extra argument 'rhs' in call error
}

swift

2022-09-30 14:04

3 Answers

In Swift 2.0

 varn=ruizyou (4.0, rhs: 2.0)

does not result in syntax errors (check Playground in Xcode.7.1.1).

In Swift 1.2

 varn=ruizyou (4.0, 2.0)

Unlabeling the second argument does not cause a syntax error (check Playground in Xcode 6.4)

Additional comments due to updated questionnaires:

It's not a problem with the function (method) format (Syntax), it's a problem with how to declare instance variables (properties).When declaring an instance variable, the initial value cannot be a nonstatic value (such as 0.0 or 1.0).

 varn=ruizyou (-9.0, rhs: 2.0)

 varn = 0.0

Then, the error will not appear.
To initialize a dynamic value using a method, use the class initiator to do so.

//Swift 2.0
import UIKit
// After importing import Foundation // UIKit, you do not need to import Foundation.

classPcScoreDistance {// Class names begin with uppercase letters.Start instance names with lowercase letters to avoid confusion.
    // A program that determines the power of a number
    funcruizyou(lhs:Double, rhs:Double) - > Double {
        // letruizyou1 = power(lhs, rhs)
        // return ruizyou1
        return power(lhs,rhs)
    }
    varn —Double = 0.0 // Initialize to 0.0 for now.

    // Initiator
    init(){
        n = self.ruizyou (-9.0, rhs:2.0) // Receiver self is required for the method.
    }
}

let object = PcScoreDistance()
object.n // Output: 81

I will come up with another amendment.This defines the method ruizyou() out of the class as an independent function.

//Swift 2.0
import UIKit

// A program that determines the power of a number
func Ruizyou(lhs:Double, rhs:Double)->Double{// Independent functions are better suited to past assets from Objective-C if you start with capital letters.
    return power(lhs,rhs)
}

classPcScoreDistance{
    varn —Double=Ruizyou (-9.0, rhs: 2.0)
}

let object = PcScoreDistance()
object.n // Output: 81


2022-09-30 14:04

I think I need a label when I call.

 varn=ruizyou (4.0, rhs: 2.0)


2022-09-30 14:04

classmodifier before func

class funcruizyou(lhs:Double, rhs:Double) - > Double {
   ...
}

So what do you think?

(Maybe varn will need class as well.)


2022-09-30 14:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.