-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.rs
More file actions
67 lines (54 loc) · 1.67 KB
/
Copy pathfunction.rs
File metadata and controls
67 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// function.rs
pub fn function() {
/*
TOPIC OF FUNCTION: -----------------------------------------------------------------
1) Defining a function
A function definition specifies what and how a specific task would be done.
2) Calling or invoking a Function
A function must be called so as to execute it.
3) Returning Functions
Functions may also return value along with control, back to the caller.
4) Parameterized Function
Parameters are a mechanism to pass values to functions.
*/
// 1) Defining a Function
// syntax:
// fn function_name(param1,param2..paramN) {
// function body
// }
fn say_hello() { // <----- making
println!("Hello Aria!");
}
// 2) Calling or invoking a Function
// syntax: function_name(val1,val2,valN)
say_hello(); // <------ calling
greet_aria();
// 4) Returning Value from a Function
/*
With return statement
Syntax 1:
fn function_name() -> return_type {
//statements
return value;
}
Shorthand syntax without return statement
Syntax 2:
fn function_name() -> return_type {
value //no semicolon means this value is returned
}
*/
println!("1 + 100 = {}", addition(1, 100));
println!();
// # Passing string to a function
let my_string: String = String::from("I'ts Aria");
display_string(my_string);
}
fn greet_aria() {
println!("Hi Aria!"); // thats not Expression
}
fn addition(number1: i32, number2: i32) -> f32 {
number1 as f32 + number2 as f32 // thats call Expression
}
fn display_string(string: String) {
println!("----> {}", string);
}