|
Hi everyone, I'm at day 7 now, and I'm a bit confuse of "return values from function". I mean, why would we use it? Can't we just use "if, else if"? why we have to write "->data type " and "return". I mean, I'm not very clear why we have to write this "returning" format/structure, but not just saying whatever we wanna say instead? what does it "returning" for?? And for example, please see this test question: func isEveryoneCanadian(_ birthCountries: [String]) { for country in birthCountries { if country != "Canada" { return false } } return true } The answer said: "his code attempts to return a value from a function that doesn't return anything." So could anybody tell me why this code won't return any value? I mean, if the country is not "Canada", show "false", else if it is "Canada", show "true". I feel this one makes sense to me. ? Thanks! Leona |
|
Sometimes functions are used to break out a calculation or other logic from the main flow of code. This helps keep your code manageable and readable. You return values as part of this. Avoiding code duplication is another good reason for pulling code into a separate function that returns a value. So, say you have some code that does something and as part of that something, you need to get a value from a calculation. You could put that calculation inline in the main body of your code, but that could get complicated if the calculation takes more than a line or two. Also, if you make the same calculation in multiple places in your app, you would have to include the same code every time. Hopefully, you don't make a mistake typing it in again! And what if you need to change your calculation? You would need to find everywhere that calculation is made and change it. Having that calculation in onne place in a single function eliminates both of those potential bugs. As for the specific example you asked about:
Even though the function body contains a couple of return statements, the function doesn't return a value and will give you a compiler error because the function declaration doesn't indicate that it returns anything. You would need to declare the function like this:
That tells the complier that this function returns a Boolean. |
|
Welcome to Day 7! When I was teaching a friend to cook a dessert, some of the steps we took just performed a simple task. For example, at one point, we had to stir the contents of a pot on low heat. That was just a simple unit of work that really didn't produce anything. It was just a task we did. But another part of the recipe required us to assemble some ingredients, and the result was a flavored icing. We had to do this three times, all similar steps, but with different ingredients. These steps produced a product, some sort of result. Functions have a similar story! You can have a function that just performs a series of work. Or, you'll find yourself writing functions that take input, perform some evaluation, or manipulation and needs to return a result! So the designers of the Swift language had to think about function structures to ensure the Swift compiler could determine if a function just did work, or performed work on some input, or accepted input, performed work AND THEN returned some sort of result.
The way you define the functions is often called the function's signature. If you have a signature that will eventually return a value (or some other object), you need to alert Swift of your intentions. The Swift compiler needs to know that you want your function to return a boolean, or a string, or an Icing object. This is why you're required to include the return statement in your code. Swift will check your signature with the data you are trying to return ensuring they are the same types. Copy this code into a Swift Playgound. (You ARE using Swift playgrounds, yes??)
|
|
Thank you so much for the detailed and paitient explanations, @roosterboy and @Obelix !! I got it now!!! Thank you! |
|
@Obilex, very clear explanation ! You always come up with good metaphor, that is quite impressive ! Boat |
|
@Obilex I love your way of teaching, too! I understand it and understand it so clearly now! thank you again, and also thanks to @roosterboy. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.