The following signatures all return an instance of the empty tuple () (typealiased as Void).
func implicitlyReturnEmptyTuple() { }
func explicitlyReturnEmptyTuple() {
return ()
}
func explicitlyReturnEmptyTupleAlt() {
return
}
In all of the three above, the return type of the function, in the function signature, has been omitted, in which case it is implicitly set to the empty tuple type, (). I.e., the following are analogous to the three above
func implicitlyReturnEmptyTuple() -> () { }
func explicitlyReturnEmptyTuple() -> () {
return ()
}
func explicitlyReturnEmptyTupleAlt() -> () {
return
}
With regard to your comment below (regarding body of implicitlyReturnEmptyTuple() where we don't explicitly return ()); from the Language Guide - Functions: Functions without return values:
Functions are not required to define a return type. Here’s a version
of the sayHello(_:) function, called sayGoodbye(_:), which prints
its own String value rather than returning it:
func sayGoodbye(personName: String) {
print("Goodbye, \(personName)!")
}
...
Note
Strictly speaking, the sayGoodbye(_:) function does still return a
value, even though no return value is defined. Functions without a
defined return type return a special value of type Void. This is
simply an empty tuple, in effect a tuple with zero elements, which can
be written as ().
Hence, we may omit return ... only for ()-return (Void-return) function, in which case a () instance will be returned implicitly.
Void.)@inline(never) func foo() { return () }withswiftc -emit-assembly -O main.swift. I am not an expert in assembly programming, but as far as I can see, no return value is put on the stack or into a return register.sizeofthe empty tuple is0, whereas thestrideofit is1(althoughstrideofis strictly positive, so this perhaps doesn't say much). Also, we can create e.g. arrays of empty tuples (as initialized e.g. by return from a function call), which are stored, but that's probably playing with non-intended features. Since type()can only hold a single unique value (()), maybe there is no need to resolve any captured()-returns from functions, as the compiler can deduce the (single unique) return value of these captures at compile time.