What is your favorite swift code


Functions let you define reusable pieces of code that fulfill a certain functionality. In many cases, functions can take values ​​to control their behavior, but this is not a must.

Let's start with a simple function:

If you write this code in your playground, nothing will be output. And yes, that is correct. The reason nothing was issued is because we put the message "My favorite album is Fearless" in a function called. This code won't be called until we've asked Swift to run the function. To do that, add the following line:

This executes (or "calls") the function, so you will also see the output "My favorite album is Fearless".

As you can see, a function is defined by typing, then the function name, then brackets, then a block of code delimited by curly braces. You call this function by writing its name and then brackets.

Of course that's a stupid example - this function does the same thing over and over again, so it has no right to exist. But what if we want to put out a different album every time? In that case we could tell Swift that our function should take a value when called and then use that value in it.

Let's do that:

This tells Swift that our function should take a value (we call this a "parameter"), which is called "name" and is a string. We then use string interpolation to write the name of the favorite album into our output. To call the function now, we write the following:

You may still be wondering what the purpose of it all is because that's still a line of code. Well, imagine we're using this feature in 20 other places in a big app. Then your head of design comes over and tells you that you got the message on "I love Fearless so much - it's my favorite album!" should change. Do you really want to search for and change all 20 instances of this message in your code? Probably not. You change a function once and everything else remains up to date.

Your functions can take as many parameters as you want, so let's take the name and year.

The names of the function parameters are important and in principle make up part of the function itself. Sometimes you will see several functions with the same name, e.g., but they have different parameter names to distinguish the different functionalities.

External and internal parameter names

Sometimes you want to name parameters in a certain way when the function is called, but in a different way in the function itself. This means that a function call should read almost like natural English, but the parameters within a function should have functional names. This technique is used a lot in Swift, so it's worth understanding this now.

To demonstrate this technique, let's write a function that returns the number of letters in a string. You get this with the attribute of strings, so we could write something like this:

This function would be called as follows:

Although it works for sure, it's a bit of a hassle. In addition, that would not be something you would say out loud in English: “count letters in string string hello”.

Swift's solution to this is that you can give the parameter one name when the function is called and another name within the function. To use this, write the parameter name twice - once for external use, once for internal use.

For example, we could name the parameter when the function is called and within the method, like this:

You can also specify an underscore,, as the external parameter name, which tells Swift that this parameter shouldn't have an external name at all. As an an example:

As you can see, this line of code can now be read like an English sentence: “count letters in string hello”.

Although there are many cases when the right choice is the one, Swift programmers generally prefer to name their parameters. And think about it: Why should we want the word "String" in the function name at all - what else should we be able to count letters from?

So what you often see are external parameter names like "in", "for" and "with" and more meaningful internal names. Hence the "Swifty" way to write this function would be as follows:

This means that you would call the function with the parameter name "in", which does not make any sense in the function itself. Therefore the parameter within called the function "string", which makes more sense. So the function can be called as follows:

And the is actually "Swifty" Code: "count letters in hello" reads like natural English, but the code itself is clear and understandable.

Return values

Swift functions can return a value by writing and then a data type after the parameter list. Once that's done Swift will make sure that the function returns a value anyway, so again here you are making a promise about what the code will do.

To give an example, let's write a function that returns true if an album was made by Taylor Swift and false if not. This should take a parameter (the name of the album in question) and return a Boolean. Here is the code:

If you want to try out your new knowledge about, this feature would be an example where that would work well.

You can now call it up by entering an album name and then processing the result:

Sponsor Hacking with Swift and reach the world's largest Swift community!