Function#
Function Definition#
Functions are created using the fn keyword as follows.
There is no need to consider the order in which functions are defined, as long as they are defined somewhere within a scope.
Therefore, in the example code below, the hello() function is located after the main() function, and it compiles and runs correctly.
fn main() {
hello();
}
fn hello() {
println!("hello");
}Parameter#
You can pass parameters when calling a function, as shown in the following example code.
The add function can take two parameters, a and b, both of type i64.
You must declare the type of each parameter, like i64, when receiving them in a function.
You can pass multiple parameters.
fn main() {
add(1, 2);
}
fn add(a: i64, b: i64) {
let sum = a + b;
println!("{}", sum);
}Let’s take a look at one more example.
The following is an example that passes a String-type string as a parameter.
fn main() {
print_text("hello".to_string());
}
fn print_text(text: String) {
println!("{}", text);
}Statement and Expression, and Semicolons ( ; )#
Rust is an expression-based language.
The use of semicolons (;) in Rust is unique, so it is important to understand it correctly.
First, let’s learn about statements and expressions.
In the following code, let apple = 1; is a statement.
A statement is a command that performs an operation but does not return a value.
fn main() {
let apple = 1;
}In the following code, 1+2 is an expression.
An expression is something that performs an operation and returns a value.
After executing 1+2, it returns 3.
fn main() {
let banana = {
1+2
};
}In the code above, it is important to note a difference from typical programming languages.
Observe the placement of the semicolon (;): it is not positioned after 1+2, but rather after the curly brace }.
Because the expression 1+2 must return a value, it does not use the ; that denotes the end of a command.
Instead, let banana = { 1+2 }; forms a single statement, so the ; must be coded after the }.
The Function’s Return and Semicolon ( ; )#
To return a value from a function, you must declare the return type after ->, as shown in apple() -> i64 in the following code.
fn main() {
let banana = apple();
println!("{}", banana);
}
fn apple() -> i64 {
return 3;
}Let’s take a closer look at the following code.
The difference from the previous code is that the return and the ; have been removed from inside the apple() function.
In Rust, this code is syntactically correct.
Because there is no ; after the number 3 within the apple() function, it is an expression, which is equivalent to returning the number 3.
That is, the previous code and the following code produce the same result.
fn main() {
let banana = apple();
println!("{}", banana);
}
fn apple() -> i64 {
3
}Let’s take a look at one more example.
In the following code, because there is no semicolon after left + right inside the add() function, it is an expression, and thus the value of left + right is returned.
fn main() {
let banana = add(1, 2);
println!("{}", banana);
}
fn add(left:i64, right:i64) -> i64 {
left + right
}In this way, the use of ; in Rust is unique.
You should not just use ; at the end of every line of code; rather, you must understand expressions correctly and use ; as needed.