Search code examples
swiftswift5swift-playground

What is the meaning of () in output console?


Problem Statement

I am new to Swift and could not seem to get what () means that keeps showing up after each function call of my class.

The Code

import UIKit


class sample{
        //Member variables
        var number = 34
        var decimal = 3.5
        var subject = ""

        //Member method

        func printSentence(){
            number += 1
            print("The value of number is \(number)")
            print("Subject name: \(subject)")
        }

}

var o: sample = sample()

print(o.subject)
print(o.printSentence())
o.subject = "Swift Programming"

print(o.subject)
print(o.printSentence())

I do have an object-oriented background but I fail to decipher what () means that shows up in the Live Viewer and the Console on running my program. (Screenshot below) screenshotXCode


Solution

  • That's coming from this line:

    print(o.printSentence())
    

    o.printSentence() is a function that returns nothing, a.k.a. () or Void.

    You probably meant to call the function, not print the function, instead:

    o.printSentence() /// executes all the code inside `printSentence()`