This tutorial is written for the variables and the constants, how to declare and use them. It also talks about the major difference between those two and indicates under which circumstance one should use a variable or a constant.
In a programming world, a variable or a scaler is used to store a piece information, called “value”, and a variable name is the reference to access the stored value.
A common syntax to declare a variable in Swift is,
var variable_name: data_type = expression
var boxLength: Double = 9.98
- The keyword “var” ( short for variable ) is used to declare a variable.
- The example above declares a variable called “boxLength”, and initializes it with a value of “9.98”.
- The “: Double” in the example is called a data type, that defines the type of data are you storing. ( It is discussed in details in the next tutorial. )
Although, a data_type ( i.e. “: Double” ) is optional if the value is provided at the declaration. Behind the scene, Swift does auto-assigning of the data types, based on the provided value.
var boxLength = 9.98
However, if the value isn’t provided, then the data_type has to be explicitly provided, and the value can be assigned later in the program.
var boxLength: Double ... ... boxLength = 9.98
The variables in Swift are mutable, which basically means that once you assign a value, you can change it anytime.
var boxLength = 5.0 boxLength = 10.0 print( boxLength ) // output -> 10.0
Multiple variables can be declared at the same time, separated by a comma ( , ) and the data type has to be specified.
var varA, varB, varC : Double
Although, variables that are declared together, has to be initialized separately.
var varA, varB, varC : Double varA = 10.0 print( varA ) // output -> 10.0
The rules of variable names,
- The variable name cannot contain white spaces, mathematical symbols, keywords, arrows.
- The variable name cannot begin with a number, although the number can be added later on the name.
- Once the variable is declared, you cannot re-declare it, nor you can change the data type.
- You can not change a variable to constant, or vice versa.
A constant is the same as a variable, the only difference is that it is immutable by nature, which means once you declare a constant you can not change the value.
The syntax to declare a constant is,
let constant_name: data_type = expression
let width: Double = 10.0
The keyword “let” declares a constant. Also, the data_type is optional, if the value is provided. In case if the value is absent, then data_type has to be explicitly provided.
let width: Double width = 10.0 print( width ) // output = 10.0
Once the constant is initialized with a value, it can’t be modified.
The example below will generate an error.
let width = 10.0 width = 15.0 print( width ) // compile time error will be thrown
Let’s create a small program that calculates the area of a rectangle.
First open your terminal and change your current directory to the ‘swift’ directory by typing ( If you don’t remember then please read the last tutorial ).
$ cd ~/swift/
Now, create a directory called “AreaCalculator”, and go into that directory,
$ mkdir AreaCalculator $ cd AreaCalculator
Now initialize a new swift package by typing in the terminal,
$ swift package init --type executable
Next, go to ‘Sources‘ directory and open up the ‘main.swift’ file in your plain-text editor, and erase all the pre-generate content from it.
To create an app that calculates the area of a rectangle, we need two variables called “length” and “width”, and the result would be stored in a constant called “area”.
var length = 10.0 var width = 15.0 let area = length * width print( area )
We used “area” to be a constant because once the area of a rectangle is calculated, it shouldn’t be altered!
Now, to compile the code,
$ swift build
To run the app,
The output should be,
In this tutorial, we talked about variables and constants. How to declare them, and the rules of the naming convention. We learned the major difference between the two, which is, variables are mutable by nature and constant aren’t. At the end of the tutorial we create a small app that uses 2 variables “length” and “width” and one constant “area” to calculate the area of a rectangle.
In the next tutorial, I will be discussing the basic data types of Swift, click here to read!