Home > front end >  Why is my Swift program printing non-existent characters?
Why is my Swift program printing non-existent characters?

Time:10-26

The following swift program defines an enumeration called tempConverter with 2 cases - Fahrenheit & Celsius. That same enumeration also defines a method convert which takes a temperature value as a Double, and returns a C to F or F to C temperature conversion, depending on what the initialized value of tempConverter.self is. It also defines a method called switchScale which simply returns both Celsius & Fahrenheit conversions for the Double provided. When calling these methods & printing their results, I am getting a series of () characters outputted after the conversion values. Here's a link to a visual of the output I am seeing, reference this if you do not want to copy & run the code yourself.

Here's the code in its entirety, I really don't know why these () characters are being displayed, or how to remove them from the output.

Any help is appreciated!

(p.s) I know I spelt Fahrenheit wrong in the program, I have no excuse & am simply stupid. Carry on.

import Foundation


enum tempConverter{
    case Celsius
    case Farenheit

        init(in scale: String){
            self = .Celsius
            scale.lowercased()
            if scale.contains("c"){
                self = .Celsius
                }
            
            else if scale.contains("f"){
                self = .Farenheit
                }
        }
    
    func convert(for temp: Double){
        switch(self){
        case .Celsius: print(temp, "c ->", (temp * 9)/5   32, "f")
            break
        case .Farenheit: print(temp, "f ->", (temp - 32) * 5 / 9, "c")
            break
        }
    }
    
    mutating func switchScale(){
        switch(self){
            
        case .Celsius:
            self = .Farenheit
            print(convert(for: temp))
            break
            
        case .Farenheit:
            self = .Celsius
            print(convert(for: temp))
            break
        }
    }
}

var instance = tempConverter(in: "f")

var temp = 32.0
print(instance.convert(for: temp), instance.switchScale())

temp = 212.0
print(instance.convert(for: temp), instance.switchScale()) 

CodePudding user response:

There is a lot going on with your code that I would change but to answer your specific question, the reason you are getting parentheses in your output is because you are printing a function.

struct SomeStruct {
    func someFunc() {}
}

let s = SomeStruct()
print(s.someFunc()) // prints ()

You do that here:

print(instance.convert(for: temp)) // prints ()

CodePudding user response:

What you see as output is that you are printing the returned value from the function call.

Functions that doesn't return a real value are said to return void and void can in swift be represented either with the keyword Void or by empty parentheses, ()

So in your case the function func convert(for temp: Double) returns void and this is printed as ()

You can actually declare the function to return void if you want to be extra clear (but you don't need to, the compiler understands you anyway) so

func convert(for temp: Double) { ... }
func convert(for temp: Double) -> Void { ... }
func convert(for temp: Double) -> () { ... }

all mean the same.

Off topic but maybe you mean for your function to return a Double so you could print that instead. Like this for example

func convert(for temp: Double) -> Double {
    switch(self){
    case .Celsius: 
        return temp * 9/5   32            
    case .Farenheit: 
        return (temp - 32) * 5 / 9
    }
}
  • Related