Clarification for return types in a function

Viewed 40

A have a few basic questions in regards to the returns a function has. Take for example the following function:

func add2(_ num1: Int, _ num2: Int) {
   print(num1 + num2)
}

Questions:

1.) There is no need for the word return before the print statement because it is just one return correct?

2.) What kind of return type would this be considered if it is just a print statement.

1 Answers

Void is a typealias for the empty tuple, i.e. no values. There is only 1 way for a tuple to be empty tuple so void values are all the same, and so hold no information.

This concept is more generally called Unit Type

You can write return as the last statement in a void function but there’s no need to, because the return value is implied. You can return any expression that is itself void (return print(“…”)) but that’s not common.

You can write -> Void or even -> () as the function’s return type but this is implicit too if omitted.

Related