Functions in Swift

Hey Guys! It’s Ashank here again. This time, we are going to talk about functions in Swift. We have covered functions in previous posts, but this time, we are going to go more in depth into the purpose of a function and different types of functions.


What is a Function? 

A function is an action that you define in classes. Once defined, any object, or method, of that specific class can effectively call that function if needed.


Parts to a Function

Consider a boss at a tech company. A boss cannot do everything by himself, so he has to hire assistants or secretaries to divide his work evenly. Now, when the boss wants his assistant to do something, the boss must call his assistant and provide the assistant with any information that he may need. After given the information, the assistant will perform the task assigned to him and report back to his boss with the results of his work.

What does this have to do with functions? Well, the boss symbolizes the object of a class named Boss. This Boss class has several methods that it can perform, which resembles assistants of a boss. But how do we give information to these assistants? Well, to the right of every function, there exists two parenthesis, signifying parameters. These parameters are the information that the boss (object) gives to the assistant (method) for the assistant to start its work! Whatever the assistant shows the boss after his task is done is the return type of the method.

Let’s jump into XCode to further explain this boss and assistant analogy.



Screen Shot 2015-10-13 at 4.47.01 PM



This is a simple class, and if you do not recall how classes work or what the init method does, please refer to the first post in the series.

So now, we will create several methods or “assistants” for the boss. These assistants will read emails, get food, manage the website, set meetings, go home, and calculate salary.. Using our basic knowledge of functions, let’s make these 5 methods without any code in them.



Screen Shot 2015-10-13 at 5.07.32 PM



As you can see, we have only merely told the Swift compiler that there are functions in this class. As of now, they have nothing inside of them.

So, if you recall the analogy, the boss needs to be able to give the assistants information so that the assistants can carry out their duties. Where does this information go? Well, it goes into the parenthesis at the ends of each function, otherwise known as the parameters of the function. So, we can put a variable or several variables as parameters of the function. Doing this will allow the assistant to know what type of information he or she is receiving.



Screen Shot 2015-10-13 at 5.08.11 PM



As you can see, we have set up variables that the boss will give to his assistants to allow them to perform their tasks. For the assistant reading the email, he will get what email number to read. For the assistant getting the food, he will receive the type of food that he has to bring. For the assistant managing the website, he will receive the name of the website that he has to manage. For the assistant setting meetings, he will get the meeting date and the meeting time. For the assistant who is going home, will receive a true or false notification from the boss to see if the assistant is going home. For the assistant who is calculating the boss’s salary, he will receive the original salary and the bonus salary from the boss.

Now that we have told the assistants what information their bosses are going to give them, we have to tell the assistants what they are bringing back to the bosses. This is otherwise known as the return type of the method. Let’s add the return types to all these methods.



Screen Shot 2015-10-13 at 5.14.37 PM



As you can notice, each function has an arrow followed by a data type following it. This tells Swift that the “readEmail” assistant is going to return a String back to the boss. Now, the “goHome” assistant’s job is to go home when the boss tells him to go home. Therefore, it does not have a return type since it will not be reporting back to the boss.

You can probably notice the red exclamation marks at the left. This is because the Swift compiler expects a return statement for those methods that you have stated are returning something. We will fix this issue momentarily.

Now that we have told the assistants what they will receive from the boss and what they will return to the boss, we now have to tell the assistants how they do it. We will now write code for each method within the curly braces to do this, as well as the return statements, which are simply “return + variable name” depending on what you want to return.

With all the added code, it looks like this:



Screen Shot 2015-10-13 at 5.40.00 PM



As you can see, simple code has been added to all of these functions to do their tasks. Now we can test these functions out by creating an instance of the Boss class.



Screen Shot 2015-10-16 at 8.56.06 PM



Now that we have an object of the Boss class, let’s trying running those methods to see what they return.



Screen Shot 2015-10-16 at 9.05.06 PM



As you can see, all the methods return the specific data type that we told the methods to return. We entered in parameters for the function so that we could get the object to do what we wanted. All of the functions use the parameters to perform the action. If you look at the goHome function called by boss1, however, you can see that the console merely outputs the information for boss1. This is because that function is not returning anything, so Swift just prints the object’s information. Also, we declared a boolean variable before calling the function “goHome” so that we could directly place that variable as a parameter.

Functions are a very diverse topic, and you can get better at understanding how they work and discovery additional functionality by practicing making functions and classes on your own! Remember that your programmming skills will become better only if you practice them! That’s it for this time!