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
€66.54
List Price:  €95.06
You save:  €28.52
£55.59
List Price:  £79.41
You save:  £23.82
CA$98.62
List Price:  CA$140.89
You save:  CA$42.27
A$107.77
List Price:  A$153.97
You save:  A$46.19
S$94.24
List Price:  S$134.63
You save:  S$40.39
HK$544.69
List Price:  HK$778.17
You save:  HK$233.47
CHF 61.97
List Price:  CHF 88.53
You save:  CHF 26.56
NOK kr777.48
List Price:  NOK kr1,110.74
You save:  NOK kr333.25
DKK kr496.36
List Price:  DKK kr709.11
You save:  DKK kr212.75
NZ$119.56
List Price:  NZ$170.81
You save:  NZ$51.24
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,365.27
List Price:  ৳11,950.91
You save:  ৳3,585.63
₹5,900.44
List Price:  ₹8,429.57
You save:  ₹2,529.12
RM312.50
List Price:  RM446.45
You save:  RM133.95
₦117,802.26
List Price:  ₦168,296.16
You save:  ₦50,493.90
₨19,453.72
List Price:  ₨27,792.22
You save:  ₨8,338.50
฿2,424.59
List Price:  ฿3,463.85
You save:  ฿1,039.26
₺2,424.79
List Price:  ₺3,464.14
You save:  ₺1,039.34
B$407.45
List Price:  B$582.09
You save:  B$174.64
R1,266.19
List Price:  R1,808.93
You save:  R542.73
Лв130.55
List Price:  Лв186.51
You save:  Лв55.96
₩97,779.29
List Price:  ₩139,690.68
You save:  ₩41,911.39
₪255.44
List Price:  ₪364.94
You save:  ₪109.49
₱4,127.53
List Price:  ₱5,896.73
You save:  ₱1,769.19
¥10,761.39
List Price:  ¥15,374.07
You save:  ¥4,612.68
MX$1,436.97
List Price:  MX$2,052.90
You save:  MX$615.93
QR254.80
List Price:  QR364.02
You save:  QR109.21
P956.33
List Price:  P1,366.25
You save:  P409.91
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,472.08
List Price:  E£4,960.32
You save:  E£1,488.24
ብር8,640.26
List Price:  ብር12,343.76
You save:  ብር3,703.50
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$68,324.23
List Price:  CLP$97,610.23
You save:  CLP$29,286
CN¥507.13
List Price:  CN¥724.50
You save:  CN¥217.37
RD$4,227.39
List Price:  RD$6,039.39
You save:  RD$1,812
DA9,355.03
List Price:  DA13,364.90
You save:  DA4,009.87
FJ$160.86
List Price:  FJ$229.81
You save:  FJ$68.95
Q540.54
List Price:  Q772.23
You save:  Q231.69
GY$14,646.31
List Price:  GY$20,924.19
You save:  GY$6,277.88
ISK kr9,657.22
List Price:  ISK kr13,796.62
You save:  ISK kr4,139.40
DH702.24
List Price:  DH1,003.24
You save:  DH301
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,122.75
List Price:  ден5,889.90
You save:  ден1,767.14
MOP$561
List Price:  MOP$801.47
You save:  MOP$240.46
N$1,262.61
List Price:  N$1,803.81
You save:  N$541.20
C$2,572.83
List Price:  C$3,675.63
You save:  C$1,102.80
रु9,443
List Price:  रु13,490.57
You save:  रु4,047.57
S/265.82
List Price:  S/379.76
You save:  S/113.94
K281.76
List Price:  K402.53
You save:  K120.77
SAR262.83
List Price:  SAR375.49
You save:  SAR112.66
ZK1,930.37
List Price:  ZK2,757.79
You save:  ZK827.42
L331.18
List Price:  L473.14
You save:  L141.95
Kč1,682.84
List Price:  Kč2,404.16
You save:  Kč721.32
Ft27,375.34
List Price:  Ft39,109.31
You save:  Ft11,733.96
SEK kr767.08
List Price:  SEK kr1,095.88
You save:  SEK kr328.79
ARS$70,438.83
List Price:  ARS$100,631.22
You save:  ARS$30,192.38
Bs485.51
List Price:  Bs693.62
You save:  Bs208.10
COP$307,934.64
List Price:  COP$439,925.49
You save:  COP$131,990.84
₡35,677.50
List Price:  ₡50,970.05
You save:  ₡15,292.54
L1,765.49
List Price:  L2,522.24
You save:  L756.75
₲545,419.77
List Price:  ₲779,204.49
You save:  ₲233,784.72
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł287.23
List Price:  zł410.35
You save:  zł123.11
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.