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
€67.87
List Price:  €96.96
You save:  €29.09
£56.33
List Price:  £80.48
You save:  £24.14
CA$101.11
List Price:  CA$144.46
You save:  CA$43.34
A$112.90
List Price:  A$161.30
You save:  A$48.39
S$95.89
List Price:  S$136.99
You save:  S$41.10
HK$544.41
List Price:  HK$777.77
You save:  HK$233.35
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr795.45
List Price:  NOK kr1,136.41
You save:  NOK kr340.95
DKK kr506.37
List Price:  DKK kr723.41
You save:  DKK kr217.04
NZ$125.48
List Price:  NZ$179.27
You save:  NZ$53.78
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,489.45
List Price:  ৳12,128.31
You save:  ৳3,638.85
₹6,003.32
List Price:  ₹8,576.54
You save:  ₹2,573.22
RM314.95
List Price:  RM449.95
You save:  RM135
₦108,169.08
List Price:  ₦154,533.88
You save:  ₦46,364.80
₨19,460.08
List Price:  ₨27,801.31
You save:  ₨8,341.22
฿2,414.58
List Price:  ฿3,449.55
You save:  ฿1,034.97
₺2,476.26
List Price:  ₺3,537.66
You save:  ₺1,061.40
B$432.69
List Price:  B$618.15
You save:  B$185.46
R1,309.65
List Price:  R1,871.01
You save:  R561.36
Лв132.94
List Price:  Лв189.93
You save:  Лв56.98
₩102,666.23
List Price:  ₩146,672.33
You save:  ₩44,006.10
₪255.59
List Price:  ₪365.15
You save:  ₪109.55
₱4,073.94
List Price:  ₱5,820.16
You save:  ₱1,746.22
¥11,002.07
List Price:  ¥15,717.92
You save:  ¥4,715.85
MX$1,443.99
List Price:  MX$2,062.93
You save:  MX$618.94
QR254.73
List Price:  QR363.91
You save:  QR109.18
P971.15
List Price:  P1,387.42
You save:  P416.26
KSh9,031.62
List Price:  KSh12,902.87
You save:  KSh3,871.24
E£3,551.78
List Price:  E£5,074.19
You save:  E£1,522.41
ብር8,922.62
List Price:  ብር12,747.15
You save:  ብር3,824.52
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$70,211.88
List Price:  CLP$100,306.99
You save:  CLP$30,095.10
CN¥512.36
List Price:  CN¥731.98
You save:  CN¥219.61
RD$4,268.13
List Price:  RD$6,097.60
You save:  RD$1,829.46
DA9,503.22
List Price:  DA13,576.62
You save:  DA4,073.39
FJ$162.81
List Price:  FJ$232.59
You save:  FJ$69.78
Q539.11
List Price:  Q770.19
You save:  Q231.08
GY$14,612.09
List Price:  GY$20,875.31
You save:  GY$6,263.22
ISK kr9,750.89
List Price:  ISK kr13,930.45
You save:  ISK kr4,179.55
DH704.78
List Price:  DH1,006.87
You save:  DH302.09
L1,300.73
List Price:  L1,858.26
You save:  L557.53
ден4,174.94
List Price:  ден5,964.46
You save:  ден1,789.51
MOP$559.86
List Price:  MOP$799.84
You save:  MOP$239.97
N$1,311.40
List Price:  N$1,873.51
You save:  N$562.11
C$2,571.06
List Price:  C$3,673.10
You save:  C$1,102.04
रु9,593.53
List Price:  रु13,705.64
You save:  रु4,112.10
S/262.27
List Price:  S/374.69
You save:  S/112.42
K279.90
List Price:  K399.88
You save:  K119.97
SAR262.85
List Price:  SAR375.52
You save:  SAR112.66
ZK1,946.04
List Price:  ZK2,780.17
You save:  ZK834.13
L337.70
List Price:  L482.45
You save:  L144.75
Kč1,708.24
List Price:  Kč2,440.45
You save:  Kč732.21
Ft28,222.76
List Price:  Ft40,319.96
You save:  Ft12,097.20
SEK kr777.78
List Price:  SEK kr1,111.16
You save:  SEK kr333.38
ARS$72,264.67
List Price:  ARS$103,239.67
You save:  ARS$30,975
Bs482.86
List Price:  Bs689.83
You save:  Bs206.97
COP$305,718.10
List Price:  COP$436,758.87
You save:  COP$131,040.76
₡35,617.57
List Price:  ₡50,884.43
You save:  ₡15,266.85
L1,775.85
List Price:  L2,537.04
You save:  L761.19
₲546,995.01
List Price:  ₲781,454.93
You save:  ₲234,459.92
$U3,074.18
List Price:  $U4,391.87
You save:  $U1,317.69
zł289.95
List Price:  zł414.23
You save:  zł124.28
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.