Lesson 1: Swift Variables and Functions

In Swift, variables are declared with either the keyword 'var' or the keyword 'let'. The difference between these two is that when a variable is declared using 'let', it cannot be altered later, meaning once you declare it, whatever value you assigned to it is set. If you tried to assign something else to it later, you would be unable to do so because the compiler would not allow you to.

For those of you not familiar with the concept of variables, a variable is a kind of placeholder for a value. Let's say you want to store the number 24 in order to do a calculation with it later. Let's also say that you have a task given to you by your father that you need to do 4 times each day, while resting roughly the same amount of hours between doing each task. And finally, let's say this task takes about an hour on average to complete. If we put each of these values into variables (placeholders) we now have 3 variables in total, containing the values 24, 4 and 1 respectively. Let's name each of these variables appropriately: 'totalHoursInDay' to hold the value 24, 'taskFrequency' to hold the value 4 and 'timeToCompleteTask' to hold the value 1. So, if you wanted to calculate how many hours you have between each task, you would say:

// first we declare the three variables
let totalHoursInDay: Int = 24
let taskFrequency: Int = 4
let timeToCompleteTask: Int = 1

// now we can calculate the amount of hours between tasks
var hoursBetweenTasks: Int = totalHoursInDay / taskFrequency + timeToCompleteTask

Voila! Now we know how long you would need to wait before starting the next task in average. We could use these 5 exact same lines every time we needed to calculate the hours, or we could use a function. A function is something that is written once, and can be reused over and over many times, without rewriting it every time. A function also may or may not take extra arguments which the user can pass to it. For example, there might be a function called 'sum' which takes two arguments in order to return the sum of the two arguments given to it. In this example however, we are going to write a function called 'calculateTimeBetweenTasks', which takes three arguments and returns the amount of hours between each task.

// function to calculate the amount of hours between tasks
func calculateTimeBetweenTasks(totalHoursInDay: Int, taskFrequency: Int, timeToCompleteTask: Int) {
    return totalHoursInDay / taskFrequency + timeToCompleteTask
}

Notice how a function is written in Swift. it begins with the word 'func', followed by the name of the function, followed by opening parenthesis, then comes the zero or more arguments separated by commas. Notice how each argument or parameter as they are sometimes called, are followed by a colon, and then the word 'Int'. I will not be covering types in this course, so if you are unfamiliar with types such as int, string, boolean and double, please read up on those before continuing. After the type of the last parameter is a closing parenthesis. To tell the compiler that you are going to start the body of the function, an opening curly braces character is used. Likewise, to end the body of the function, a closing curly braces character is used. Inside the body of our func you will of course notice that the three parameters passed to our func is used in the same calculation as before, only now the result is returned. In order to catch the result, we will need to call this function from somewhere else.

var hoursBetweenTasks = calculateTimeBetweenTasks(24, 4, 1)

Now we have the result stored in a variable and our function can be reused wherever we want. A fitting end to this part of the course. Now you are ready to continue to Lesson 2: Collections.

Comments

Popular Posts