Learn how to return a `void` function from another function in Swift without confusion. Understand how Swift's return types work with this engaging guide.
---
This video is based on the question https://stackoverflow.com/q/64900491/ asked by the user 'Jacks Orlen' ( https://stackoverflow.com/u/14664414/ ) and on the answer https://stackoverflow.com/a/64900558/ provided by the user 'EmilioPelaez' ( https://stackoverflow.com/u/725628/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Return void func from func in Swift
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Void Functions in Swift
If you’re new to Swift programming, you might encounter some confusing concepts, especially when it comes to function return types. One common question for beginners is: How do I return a void function from another function in Swift?
In programming, a void function typically means that the function does not return a value. In Java terminology, it indicates that nothing is being returned. However, Swift has its own way of handling void, which can lead to some misunderstandings. In this guide, we'll clear up these concepts using a practical example.
The Problem
You may have written a function that looks like this:
[[See Video to Reveal this Text or Code Snippet]]
Upon running this code, you might find the output a bit puzzling:
[[See Video to Reveal this Text or Code Snippet]]
You see () printed, which represents that the function has returned Void. But why is that? Let’s break this down.
The Explanation
Understanding Function Returns: In Swift, when you declare a function like returnVoidFunc, you are correctly indicating that it will return another function of type () -> (), which is Swift’s way of saying it returns a function that takes no parameters and returns nothing (or Void).
Executing the Inner Function: The function innerOne is successfully defined inside returnVoidFunc. When returnVoidFunc() is called, it returns a reference to innerOne, which is stored in initFunc.
Calling initFunc: When you call print(initFunc()), the initFunc executes the innerOne function. Thus, "Hello from inside" is printed to the console. However, since innerOne does not return any value, the result of initFunc() is Void, which Swift represents as ().
Understanding Void in Swift: In Swift, Void is synonymous with an empty tuple (). Whenever you see (), it signifies that a function does not have a return value. That is why it showed up in the output after printing "Hello from inside".
Conclusion
The code you have written correctly demonstrates how to return a void function from another function in Swift. You observed the () because it indicates that the returned function does not provide any value back.
Key Takeaways:
In Swift, Void is represented as ().
A function returning Void still performs actions (like printing), but returns nothing.
Always remember that execution of a function returning Void will not yield additional data—only the output of execution will be visible.
By understanding these fundamentals, you can confidently work with function return types in Swift!
Информация по комментариям в разработке