Functions

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
€65.64
List Price:  €93.77
You save:  €28.13
£56.58
List Price:  £80.84
You save:  £24.25
CA$96.26
List Price:  CA$137.53
You save:  CA$41.26
A$109.08
List Price:  A$155.84
You save:  A$46.75
S$95.27
List Price:  S$136.11
You save:  S$40.83
HK$548.12
List Price:  HK$783.07
You save:  HK$234.94
CHF 63.72
List Price:  CHF 91.04
You save:  CHF 27.31
NOK kr771.14
List Price:  NOK kr1,101.67
You save:  NOK kr330.53
DKK kr490.05
List Price:  DKK kr700.10
You save:  DKK kr210.05
NZ$118.86
List Price:  NZ$169.82
You save:  NZ$50.95
د.إ257.05
List Price:  د.إ367.23
You save:  د.إ110.18
৳7,664.43
List Price:  ৳10,949.65
You save:  ৳3,285.22
₹5,835.09
List Price:  ₹8,336.20
You save:  ₹2,501.11
RM334.83
List Price:  RM478.35
You save:  RM143.52
₦90,777.03
List Price:  ₦129,687.03
You save:  ₦38,910
₨19,435.77
List Price:  ₨27,766.58
You save:  ₨8,330.81
฿2,575.67
List Price:  ฿3,679.68
You save:  ฿1,104.01
₺2,273.66
List Price:  ₺3,248.23
You save:  ₺974.56
B$364.16
List Price:  B$520.25
You save:  B$156.09
R1,350.44
List Price:  R1,929.28
You save:  R578.84
Лв128.28
List Price:  Лв183.27
You save:  Лв54.98
₩96,200.90
List Price:  ₩137,435.75
You save:  ₩41,234.85
₪263.23
List Price:  ₪376.07
You save:  ₪112.83
₱4,029.53
List Price:  ₱5,756.72
You save:  ₱1,727.18
¥10,822.20
List Price:  ¥15,460.95
You save:  ¥4,638.75
MX$1,196.86
List Price:  MX$1,709.87
You save:  MX$513.01
QR254.81
List Price:  QR364.04
You save:  QR109.22
P969.97
List Price:  P1,385.73
You save:  P415.76
KSh9,183.54
List Price:  KSh13,119.90
You save:  KSh3,936.36
E£3,382.61
List Price:  E£4,832.51
You save:  E£1,449.90
ብር3,968.10
List Price:  ብር5,668.96
You save:  ብር1,700.85
Kz58,581.63
List Price:  Kz83,691.63
You save:  Kz25,110
CLP$66,657.14
List Price:  CLP$95,228.57
You save:  CLP$28,571.42
CN¥506.67
List Price:  CN¥723.85
You save:  CN¥217.17
RD$4,155.65
List Price:  RD$5,936.90
You save:  RD$1,781.25
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$159.39
List Price:  FJ$227.72
You save:  FJ$68.32
Q543.16
List Price:  Q775.98
You save:  Q232.81
GY$14,611.27
List Price:  GY$20,874.15
You save:  GY$6,262.87
ISK kr9,886.08
List Price:  ISK kr14,123.58
You save:  ISK kr4,237.50
DH707.48
List Price:  DH1,010.74
You save:  DH303.25
L1,253.52
List Price:  L1,790.82
You save:  L537.30
ден4,041.45
List Price:  ден5,773.75
You save:  ден1,732.29
MOP$563.44
List Price:  MOP$804.95
You save:  MOP$241.50
N$1,341.89
List Price:  N$1,917.07
You save:  N$575.18
C$2,586.83
List Price:  C$3,695.63
You save:  C$1,108.80
रु9,327.64
List Price:  रु13,325.78
You save:  रु3,998.13
S/260.57
List Price:  S/372.26
You save:  S/111.68
K265.99
List Price:  K380
You save:  K114.01
SAR262.56
List Price:  SAR375.10
You save:  SAR112.54
ZK1,793.08
List Price:  ZK2,561.66
You save:  ZK768.57
L326.81
List Price:  L466.89
You save:  L140.08
Kč1,658.72
List Price:  Kč2,369.71
You save:  Kč710.98
Ft25,891.40
List Price:  Ft36,989.30
You save:  Ft11,097.90
SEK kr763.84
List Price:  SEK kr1,091.25
You save:  SEK kr327.40
ARS$60,978.78
List Price:  ARS$87,116.28
You save:  ARS$26,137.50
Bs483.61
List Price:  Bs690.90
You save:  Bs207.29
COP$274,351.87
List Price:  COP$391,948.05
You save:  COP$117,596.17
₡35,072.06
List Price:  ₡50,105.09
You save:  ₡15,033.03
L1,724.15
List Price:  L2,463.18
You save:  L739.02
₲516,670.58
List Price:  ₲738,132.47
You save:  ₲221,461.88
$U2,687.21
List Price:  $U3,839.04
You save:  $U1,151.82
zł283.10
List Price:  zł404.45
You save:  zł121.34
Already have an account? Log In

Transcript

Let's talk about functions. You've seen the main function quite a bit already. Let's talk about functions. In general functions are defined using the fun keyword. fn is pronounced fun. The rust style guide says to use snake case for function names, lowercase words separated by underscores.

One awesome thing about rust is that functions don't have to appear in the file before code that calls them. So feel free to leave main at the top of your main.rs file if you like Hooray. For more chronologically arranged source code systems programmers can have nice things to function parameters are always defined with name colon type, and of course multiple parameters are separated by a comma. You specify the return type after the parameters by adding an arrow pointing to the return type, the arrow is a hyphen and then a greater than symbol and the body of the function is inside a block. You can return a value from a function using the returns keyword as you would expect, there's also a shorthand for returning values. If you leave the semi colon off of the last expression in a block, then it will be returned as the value of the block.

This is called a tail expression. In other words, this is the same as this whenever you are returning something at the end of the block the shorter ways preferred in idiomatic rust. calling a function looks the same as in most of the languages. there's currently no support for named arguments at the call site, so you need to provide all the values in the correct order. Finally, a single rust function does not support variable numbers of arguments or different types for the same argument. But macros such as print line do a macro call looks just like a function call except the name of a macro always ends with an exclamation mark.

We will use a couple common macros but I won't be teaching you how to create them anytime soon. That's a more advanced topic. In the next video, we'll go over the module system

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.