3 You've already seen one function so far, the `main` function:
10 This is the simplest possible function declaration. As we mentioned before,
11 `fn` says "this is a function," followed by the name, some parentheses because
12 this function takes no arguments, and then some curly braces to indicate the
13 body. Here's a function named `foo`:
20 So, what about taking arguments? Here's a function that prints a number:
23 fn print_number(x: i32) {
24 println!("x is: {}", x);
28 Here's a complete program that uses `print_number`:
35 fn print_number(x: i32) {
36 println!("x is: {}", x);
40 As you can see, function arguments work very similar to `let` declarations:
41 you add a type to the argument name, after a colon.
43 Here's a complete program that adds two numbers together and prints them:
50 fn print_sum(x: i32, y: i32) {
51 println!("sum is: {}", x + y);
55 You separate arguments with a comma, both when you call the function, as well
56 as when you declare it.
58 Unlike `let`, you _must_ declare the types of function arguments. This does
63 println!("x is: {}", x + y);
70 hello.rs:5:18: 5:19 expected one of `!`, `:`, or `@`, found `)`
71 hello.rs:5 fn print_number(x, y) {
74 This is a deliberate design decision. While full-program inference is possible,
75 languages which have it, like Haskell, often suggest that documenting your
76 types explicitly is a best-practice. We agree that forcing functions to declare
77 types while allowing for inference inside of function bodies is a wonderful
78 sweet spot between full inference and no inference.
80 What about returning a value? Here's a function that adds one to an integer:
83 fn add_one(x: i32) -> i32 {
88 Rust functions return exactly one value, and you declare the type after an
89 "arrow," which is a dash (`-`) followed by a greater-than sign (`>`).
91 You'll note the lack of a semicolon here. If we added it in:
94 fn add_one(x: i32) -> i32 {
99 We would get an error:
102 error: not all control paths return a value
103 fn add_one(x: i32) -> i32 {
107 help: consider removing this semicolon:
112 Remember our earlier discussions about semicolons and `()`? Our function claims
113 to return an `i32`, but with a semicolon, it would return `()` instead. Rust
114 realizes this probably isn't what we want, and suggests removing the semicolon.
116 This is very much like our `if` statement before: the result of the block
117 (`{}`) is the value of the expression. Other expression-oriented languages,
118 such as Ruby, work like this, but it's a bit unusual in the systems programming
119 world. When people first learn about this, they usually assume that it
120 introduces bugs. But because Rust's type system is so strong, and because unit
121 is its own unique type, we have never seen an issue where adding or removing a
122 semicolon in a return position would cause a bug.
124 But what about early returns? Rust does have a keyword for that, `return`:
127 fn foo(x: i32) -> i32 {
128 if x < 5 { return x; }
134 Using a `return` as the last line of a function works, but is considered poor
138 fn foo(x: i32) -> i32 {
139 if x < 5 { return x; }
145 The previous definition without `return` may look a bit strange if you haven't
146 worked in an expression-based language before, but it becomes intutive over
147 time. If this were production code, we wouldn't write it in that way anyway,
151 fn foo(x: i32) -> i32 {
160 Because `if` is an expression, and it's the only expression in this function,
161 the value will be the result of the `if`.
163 There are some additional ways to define functions, but they involve features
164 that we haven't learned about yet, so let's just leave it at that for now.