Scope

2 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€59.63
List Price:  €85.19
You save:  €25.56
£52.34
List Price:  £74.77
You save:  £22.43
CA$96.44
List Price:  CA$137.78
You save:  CA$41.33
A$105.21
List Price:  A$150.31
You save:  A$45.09
S$90.43
List Price:  S$129.19
You save:  S$38.76
HK$544.44
List Price:  HK$777.80
You save:  HK$233.36
CHF 55.71
List Price:  CHF 79.59
You save:  CHF 23.87
NOK kr709.51
List Price:  NOK kr1,013.63
You save:  NOK kr304.12
DKK kr445.26
List Price:  DKK kr636.11
You save:  DKK kr190.85
NZ$120.58
List Price:  NZ$172.27
You save:  NZ$51.68
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,539.66
List Price:  ৳12,200.05
You save:  ৳3,660.38
₹6,340
List Price:  ₹9,057.54
You save:  ₹2,717.53
RM286.77
List Price:  RM409.68
You save:  RM122.91
₦101,425.71
List Price:  ₦144,900.08
You save:  ₦43,474.37
₨19,583.60
List Price:  ₨27,977.78
You save:  ₨8,394.17
฿2,200.79
List Price:  ฿3,144.12
You save:  ฿943.33
₺2,980.17
List Price:  ₺4,257.57
You save:  ₺1,277.39
B$379.30
List Price:  B$541.88
You save:  B$162.58
R1,181.11
List Price:  R1,687.37
You save:  R506.26
Лв116.64
List Price:  Лв166.64
You save:  Лв49.99
₩103,202.35
List Price:  ₩147,438.25
You save:  ₩44,235.90
₪225.57
List Price:  ₪322.26
You save:  ₪96.68
₱4,137.45
List Price:  ₱5,910.90
You save:  ₱1,773.45
¥10,901.29
List Price:  ¥15,573.94
You save:  ¥4,672.64
MX$1,260.79
List Price:  MX$1,801.20
You save:  MX$540.41
QR254.67
List Price:  QR363.83
You save:  QR109.16
P925.89
List Price:  P1,322.75
You save:  P396.86
KSh9,011.01
List Price:  KSh12,873.43
You save:  KSh3,862.41
E£3,311.46
List Price:  E£4,730.87
You save:  E£1,419.40
ብር10,918.61
List Price:  ብር15,598.69
You save:  ብር4,680.07
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$63,886.91
List Price:  CLP$91,270.93
You save:  CLP$27,384.01
CN¥493.74
List Price:  CN¥705.37
You save:  CN¥211.63
RD$4,442.34
List Price:  RD$6,346.47
You save:  RD$1,904.13
DA9,074.91
List Price:  DA12,964.71
You save:  DA3,889.80
FJ$159
List Price:  FJ$227.15
You save:  FJ$68.15
Q535.22
List Price:  Q764.64
You save:  Q229.41
GY$14,619.51
List Price:  GY$20,885.90
You save:  GY$6,266.39
ISK kr8,831.05
List Price:  ISK kr12,616.33
You save:  ISK kr3,785.27
DH642.87
List Price:  DH918.43
You save:  DH275.55
L1,181.28
List Price:  L1,687.62
You save:  L506.33
ден3,667.68
List Price:  ден5,239.78
You save:  ден1,572.09
MOP$560.34
List Price:  MOP$800.52
You save:  MOP$240.18
N$1,178.95
List Price:  N$1,684.29
You save:  N$505.33
C$2,571.85
List Price:  C$3,674.23
You save:  C$1,102.37
रु10,111.14
List Price:  रु14,445.11
You save:  रु4,333.96
S/235.26
List Price:  S/336.11
You save:  S/100.84
K301.22
List Price:  K430.33
You save:  K129.11
SAR262.61
List Price:  SAR375.18
You save:  SAR112.56
ZK1,612.47
List Price:  ZK2,303.63
You save:  ZK691.15
L303.46
List Price:  L433.54
You save:  L130.07
Kč1,446.63
List Price:  Kč2,066.70
You save:  Kč620.07
Ft22,861.23
List Price:  Ft32,660.30
You save:  Ft9,799.07
SEK kr648.80
List Price:  SEK kr926.89
You save:  SEK kr278.09
ARS$100,357.17
List Price:  ARS$143,373.53
You save:  ARS$43,016.36
Bs482.84
List Price:  Bs689.81
You save:  Bs206.96
COP$266,135.40
List Price:  COP$380,209.72
You save:  COP$114,074.32
₡34,954.75
List Price:  ₡49,937.50
You save:  ₡14,982.75
L1,839.74
List Price:  L2,628.31
You save:  L788.57
₲469,383.81
List Price:  ₲670,577.05
You save:  ₲201,193.23
$U2,742.46
List Price:  $U3,917.97
You save:  $U1,175.50
zł251.67
List Price:  zł359.54
You save:  zł107.87
Already have an account? Log In

Transcript

Variables have a scope, which is the place in the code that you are allowed to use them. The scope of a variable begins where it is created and extends to the end of the block along the way it is accessible from nested blocks. A block is a collection of statements inside curly braces that includes function bodies. In this example, x is defined in the main functions block. Then we create a nested block. Inside the nested block, we create y and then we print x and y which works just fine because x is accessible from nested blocks.

Then the block ends and why is immediately dropped at this point. There is no garbage collector values are always immediately dropped when they go out of scope, which means the last print won't work but fear not. We discover this at compile time. The error messages pretty clear cannot find value y in this scope, so we would either need to hoist the second print line macro up into the same scope as y, or move y down to the same scope as the second print line macro variables can also be shadowed. Another way to think about shadowing is the variables are always local to their scope. Here we create a variable x and initialize it to five in the outer block.

Then in the inner block x is shadowed with a new value 99. These two x's are different variables with different values, they just overlap in scope. The first print we'll see x as 99. Note that the value of the first x is not accessible from the inner block after it is shadowed. But as soon as the inner block ends, the inner x is dropped and the outer x is once again accessible so the last print sees the value five. You can also shadow variables in the same scope.

Here we shadow a mutable variable x with an immutable variable x that we initialize to the first access value. This essentially redefines the variable x with different mutability, even cooler, the compiler will often optimize away this actual operation. And so nothing actually happens in the assembly code. You can even shadow a variable to a different type in the same scope, which some people like to do and data transformation pipelines that discard intermediate representations. So in this example, meme starts as the words more cowbell and then becomes an image. In the next video, we'll go over memory, safety.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.