4

Let's take a look at this function:

func nothing(){
    return
}

it does not return a value, but we can write return statement how Swift handle that ? what happens behind the scene does return without value returns 0 or any value to handle this case ?

12
  • 2
    As @zneak said, it just means "return control to the caller". (However, in Swift, functions without return value have an implicit return value of type Void.) Commented Aug 14, 2016 at 20:37
  • 2
    “A return statement occurs in the body of a function or method definition and causes program execution to return to the calling function or method. Program execution continues at the point immediately following the function or method call.” Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).” iBooks. itun.es/de/jEUH0.l Commented Aug 14, 2016 at 20:40
  • 2
    “When a return statement is not followed by an expression, it can be used only to return from a function or method that does not return a value (that is, when the return type of the function or method is Void or ()).” Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2.2).” iBooks. itun.es/de/jEUH0.l Commented Aug 14, 2016 at 20:42
  • 1
    @dfri: I have inspected the generated assembly code of @inline(never) func foo() { return () } with swiftc -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. Commented Aug 14, 2016 at 21:10
  • 1
    @MartinR I see, thanks. Interestingly, the sizeof the empty tuple is 0, whereas the strideof it is 1 (although strideof is 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. Commented Aug 14, 2016 at 21:20

3 Answers 3

11

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.

Sign up to request clarification or add additional context in comments.

3 Comments

that's interesting, I am curious about the first case func implicitlyReturnEmptyTuple() -> () { } we did not write return and it expect a return () or return
That's what I wanted to understand
@iOSGeek happy to help. See also this Q&A for a small discussion of instances of the empty tuple.
5

In that case, return only means "leave the function".

2 Comments

yes I know but I wanted to understand how it works, any further explanation how Swift deal with this case and the case where there is a value
I'm not sure I understand your question. At the lowest level, the compiler knows where the calling function expects to find a return value if there's one, and it builds your function to write its return value there. If there isn't, then nothing gets written in that location and the caller doesn't try to read from it anyway.
0

Functions are first class objects in Swift . They have a type

func nothing(){
    return
}

Actual representation of the above function is like this

func nothing()->(){
        return
    }

In the above code the type of the function is an empty tuple

"()"

If you assign the above function to some variable , we can see the returned tuple

enter image description here

Comments

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.