1

The first function below works fine to check for FM values, but when I added some validation in an else if statement for the AM values, I am getting error message saying: Missing return in a function expected to return Int. It doesn't seem to be an issue with the placement of the curly braces.

        static var minAMFFrequency: Double = 520.0
        static var maxAMFFrequency: Double = 1610.0
        static var minFMFFrequency: Double = 88.3
        static var maxFMFFrequency: Double = 107.9

    func isBandFM() -> Int {
        if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
            return 1 //FM
        } else {
            return 0 //AM
        }
    }

Modified function with error:

func isBandFM() -> Int {
        if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
            return 1 //FM
        } else if frequency >= RadioStation.minAMFFrequency && frequency <= RadioStation.maxAMFFrequency{
            return 0 //AM
        }
    }
2
  • 1
    What should isBandFM() return if frequency is neither in the AM or FM band? The compiler can't guess for you. Commented Mar 27, 2020 at 20:07
  • 2
    If you end up writing something like return 1 //FM, that's a code smell. You knew the 1 is unclear, so you felt the need to explain it with a comment. Instead, you should consider replacing Int (which is a very "weak" type in this context), with something "stronger" and more self-descriptive, like enum. I suggest you check out youtube.com/watch?v=_S6UOrwS-Tg Commented Mar 27, 2020 at 20:21

2 Answers 2

2

You also need to consider cases that do not fall in the 2 conditions. It expects you to provide a default return value.

In the first case, you had a default value of 0 being returned.

In the second case, if your frequency is neither in the first range (specified by the first if condition) nor in the second range (specified by the second if condition), you need to specify a default return value.

func isBandFM() -> Int {
        if frequency >= RadioStation.minFMFFrequency && frequency <= RadioStation.maxFMFFrequency {
            return 1 //FM
        } else if frequency >= RadioStation.minAMFFrequency && frequency <= RadioStation.maxAMFFrequency{
            return 0 //AM
        }

        return 0   // or whatever value you want to return if frequency is not within FM range or AM range
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Sanil - this fixed the error and I'll include code here to cover any out of bounds values.
1

Sanil already explained the issue here, but I'll suggest some other improvements

This is better expressed using the appropriate types:

  1. A Range<Double> instead of two seperate Doubles
  2. A Boolean instead of a 0/1 Int.
  3. An enum to represent named frequency bands, like "AM", "FM", or "other"
struct RadioStation {
    enum Band {
        case am, fm, other
    }
    static let amFrequencyRange = 520.0...610.0
    static let fmFrequencyRange = 88.3...107.9

    let frequency: Double

    var isFM: Bool { RadioStation.fmFrequencyRange.contains(frequency) }
    var isAM: Bool { RadioStation.amFrequencyRange.contains(frequency) }

    var band: Band {
        if self.isFM { return .fm }
        else if self.isAM { return .am }
        else { return .other }
    }
}

6 Comments

Thank you for taking the trouble. Really appreciate your help and this amazing refactoring. It's beyond helpful!
Can I change the struct to be a class? The idea is that RadioStation is the class definition, from which I create an FM and an AM station also. Can I do this with a struct as opposed to a class?
Why do you need it to be a class? "The idea is that RadioStation is the class definition, from which I create an FM and an AM station also" yep. You should read about structs, and the Swift langauge guide more generally: docs.swift.org/swift-book/LanguageGuide/…
I thought classes were for subclassing when you want to create separate instances based on the class definition? I need to learn about passing by value vs by reference.
None of that is specific to classes. Please read the Swift language guide.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.