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
€59.89
List Price:  €85.57
You save:  €25.67
£52.68
List Price:  £75.27
You save:  £22.58
CA$96.24
List Price:  CA$137.50
You save:  CA$41.25
A$107.05
List Price:  A$152.93
You save:  A$45.88
S$89.86
List Price:  S$128.38
You save:  S$38.52
HK$545.76
List Price:  HK$779.69
You save:  HK$233.93
CHF 56.96
List Price:  CHF 81.37
You save:  CHF 24.41
NOK kr704.01
List Price:  NOK kr1,005.77
You save:  NOK kr301.76
DKK kr447.07
List Price:  DKK kr638.70
You save:  DKK kr191.63
NZ$118.79
List Price:  NZ$169.71
You save:  NZ$50.92
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,497.01
List Price:  ৳12,139.10
You save:  ৳3,642.09
₹6,170.83
List Price:  ₹8,815.86
You save:  ₹2,645.02
RM295.70
List Price:  RM422.45
You save:  RM126.75
₦107,222.90
List Price:  ₦153,182.14
You save:  ₦45,959.23
₨19,823.65
List Price:  ₨28,320.71
You save:  ₨8,497.06
฿2,264.59
List Price:  ฿3,235.27
You save:  ฿970.67
₺2,876.58
List Price:  ₺4,109.58
You save:  ₺1,233
B$380.14
List Price:  B$543.08
You save:  B$162.94
R1,235.53
List Price:  R1,765.13
You save:  R529.59
Лв117.04
List Price:  Лв167.21
You save:  Лв50.16
₩97,214.01
List Price:  ₩138,883.11
You save:  ₩41,669.10
₪233.25
List Price:  ₪333.23
You save:  ₪99.98
₱3,995.58
List Price:  ₱5,708.22
You save:  ₱1,712.64
¥10,290.97
List Price:  ¥14,702.02
You save:  ¥4,411.05
MX$1,305.67
List Price:  MX$1,865.32
You save:  MX$559.65
QR254.68
List Price:  QR363.84
You save:  QR109.16
P940.72
List Price:  P1,343.95
You save:  P403.22
KSh9,027.58
List Price:  KSh12,897.10
You save:  KSh3,869.51
E£3,357.12
List Price:  E£4,796.09
You save:  E£1,438.97
ብር9,951.53
List Price:  ብር14,217.09
You save:  ብር4,265.55
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$67,622.76
List Price:  CLP$96,608.09
You save:  CLP$28,985.32
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,400.31
List Price:  RD$6,286.43
You save:  RD$1,886.11
DA9,080.64
List Price:  DA12,972.90
You save:  DA3,892.26
FJ$157.85
List Price:  FJ$225.51
You save:  FJ$67.66
Q535.60
List Price:  Q765.18
You save:  Q229.57
GY$14,613.01
List Price:  GY$20,876.62
You save:  GY$6,263.61
ISK kr8,563.48
List Price:  ISK kr12,234.07
You save:  ISK kr3,670.58
DH629.93
List Price:  DH899.93
You save:  DH270
L1,164.15
List Price:  L1,663.14
You save:  L498.99
ден3,684.99
List Price:  ден5,264.49
You save:  ден1,579.50
MOP$561.05
List Price:  MOP$801.54
You save:  MOP$240.48
N$1,240.36
List Price:  N$1,772.03
You save:  N$531.66
C$2,571.43
List Price:  C$3,673.64
You save:  C$1,102.20
रु9,854.29
List Price:  रु14,078.16
You save:  रु4,223.87
S/247.31
List Price:  S/353.31
You save:  S/106
K295.70
List Price:  K422.46
You save:  K126.75
SAR262.63
List Price:  SAR375.20
You save:  SAR112.57
ZK1,648.02
List Price:  ZK2,354.41
You save:  ZK706.39
L303.89
List Price:  L434.15
You save:  L130.26
Kč1,464.79
List Price:  Kč2,092.65
You save:  Kč627.85
Ft23,780.13
List Price:  Ft33,973.07
You save:  Ft10,192.94
SEK kr662.21
List Price:  SEK kr946.06
You save:  SEK kr283.84
ARS$93,215
List Price:  ARS$133,170
You save:  ARS$39,954.99
Bs482.84
List Price:  Bs689.81
You save:  Bs206.96
COP$281,463.10
List Price:  COP$402,107.38
You save:  COP$120,644.27
₡35,283.96
List Price:  ₡50,407.82
You save:  ₡15,123.85
L1,829.53
List Price:  L2,613.72
You save:  L784.19
₲505,187.47
List Price:  ₲721,727.32
You save:  ₲216,539.85
$U2,800.03
List Price:  $U4,000.21
You save:  $U1,200.18
zł255.38
List Price:  zł364.85
You save:  zł109.46
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.