Saturday, October 22, 2016

Swift 3 - Functions In Swift 3


Functions in Swift 3 allow us to organize our code much more efficiently. A primary goal whenever you code in any language is to not write the same line of code more than once. and functions are big part of helping us achieve that. Let's see how they work by implementing a function inside of our playground!.

Now so far we have the following while-loop that goes round and round it. Now what if we wanted to write a while-loop they did the same thing again?. Well f we would to copy all of that while-loop code, that is very bad thing.


So let's create a function that uses that while-loop. And the way we create a function in Swift 3 is by using the keyword "func" and followed by name of the function and some brackets then curly brackets like the following example of code:


Okay, we've got that. Now the first thing you're going to notice is that there is nothing printed out on the right-hand side. And that because of the function doesn't run as default, you actually have to do what's called "Calling the Function". And the way we called is by its name and followed by some brackets like the following example of code:


And now the function will get called execute and that loop should go around as many time as it needs to.

Now the reason we have the two brackets (open and close bracket) after write name of the functions while we call the functions can pass over values of variables or others item to themselves. And what do I mean by this?. Well, let's look at a basic idea of it. Let's go to our function of checkCount() and we're going to pass over a variable called message and this is type of String like the following example of code:


Ao according to the above code, it will print out Ask MHS 10 times. So the result will look like this picture :


Now the advantages of doing like that is we can now run checkCount() and send the other message with the different value. So just write under the the last of our code in our playground checkCount(message: "Swift 3"). And it will look like this picture:
Now look how is executed Ask MHS and Swift 3. And the beauty of that, is that look we've only written two lines of code and we've the same function but we've gotten a different result. So that the fantastic thing about functions.

Now we can pass more than one parameter. So inside of our function let's have something we pass over like the following example of code:
That's bit of a contrived example, but I think you get the idea. Now the other thing is always call your function something obvious checkCount, I've got a verb of got a noun and it's telling you exactly what it is doing. Always make sure it is descriptive, don't make it too long or too short but descriptive nonetheless.

Now the final part of functions is the fact that they can return something. And the way we specify them returning something is with a -> then followed by specify the type of thing it's going to return. It could be a String, Integer or Boolean, etc., whatever you like. And don't forget to give return into what variable will be returned the value.
Now our checkCount receives back a String and you can see that we have "Done!" printed out with it on the right-hand side of our playground. So it's a good way of passing back some kind of message from our Strings or from our functions rather.

That's all about Functions In Swift 3. In the next tutorial we will learn about Optional in Swift 3, so just stay tune on Ask MHS. If you have any questions, just leave some comments below and I'll reply it soon. Thank you very much for visit my blog. See you next time in the next tutorial.

No comments:

Post a Comment