Print

Swift Tutorial: Introduction 03 - Scope and Semicolons

Written by Paul Napier on . Posted in Swift Tutorials: Introduction

Hello everyone and welcome to the third tutorial in Swift programming brought to you by MadApper. once again I am Paul Napier and in this lesson we are just going to cover off a couple of syntactical points in Swift: scope and semicolons.

In the last lesson, we learned about variables and constants, how to declare them and what they mean. In this less we are going to look at this code below:

let count = 10
var startVariable = 0
for (var a = 0; a<count; a++){
    var add = 0; var multiply = 0
    add = a+10
    if (a%2==1){
        var factor = a*a
        multiply = factor
    }
    var calculation = add + multiply
    startVariable += calculation
}
var finalVariable = startVariable

Don't worry about this code too much, but suffice it to say, it has some variables and some functions. We will cover all of this later on, but the most important thing you need to understand is that this variable at the top, startVariable, is created, manipulated through this block of code and the result is passed across to finalVariable at the end.

Ok so let's talk scope. Scope is effectively the lifespan of a variable. For instance, we have a number of variables created throughout the code, but they are not all accessible throughout every part of the code. Different code blocks are divided up into nested blocks using curly braces. You will see these more and more as we progress. As a rule of thumb you can say that what is declared before one set of curly braces is accessible within, whereas what is declared inside is not accessible outside.

So if I try to access the variable 'add' outside this loop, you can see that it throws an error.

let count = 10
var startVariable = 0
for (var a = 0; a<count; a++){
    var add = 0; var multiply = 0
    add = a+10
    if (a%2==1){
        var factor = a*a
        multiply = factor
    }
    var calculation = add + multiply
    startVariable += calculation
}
var finalVariable = startVariable

add

That is scope. Try accessing the variable 'multiply' below the { closing the if statement to see whether you get an error.

This is an important concept in Swift and throughout programming in general. Over time you will become adept and knowing what variables you need and when. As you progress you will start to address concepts such as memory management, of which scope forms an important aspect.

So let's just quickly finish today's tutorial on Swift by pointing out semicolons. If you have come from another language such as Java or Objective C, then you will be familiar with the fact that the semicolon ends a statement. Before you can begin another statement then you have to add the semicolon, or the IDE will throw an error. In Swift, however, that is not the case. You can write a line of code, such as declaring a variable, move to the next line and write another line.

That is not to say that if you want to, you cannot add a semicolon. You can. For example, I can add a semi colon to the end of the statement declaring the constant count, and the compiler will accept it. However, it is optional, and makes no difference to the way the code is complied.

There is one instance, however, when semicolons are required. If you want to write multiple statements on a single line, then each statement must be separated by a semicolon. If I try to assign a value to each of these variables on the same line, then you will see that it throws an error and I have to add the semicolon to separate it before the code will compile.

var add = 0; var multiply = 0

This is an example where I have written two statements on a single line. 

Ok, in the next lesson we are going to finish off the preparatory sessions and cover commenting your code in Swift.

Add comment


Security code
Refresh