summaryrefslogtreecommitdiff
path: root/old_curriculum/functions
diff options
context:
space:
mode:
authorolivia <olivia@fastmail.com>2018-04-26 21:29:11 +0200
committerolivia <olivia@fastmail.com>2018-04-26 21:29:11 +0200
commit5e89d1e888a8fafc39096afce36d02e313f349c2 (patch)
tree97b13f8223012d1db88d510cd79ea0e49570d1f7 /old_curriculum/functions
parent32ac403da5c49b002ba420186d3122501196ff89 (diff)
move old files to a separate directory
Diffstat (limited to 'old_curriculum/functions')
-rw-r--r--old_curriculum/functions/functions1.rs44
-rw-r--r--old_curriculum/functions/functions2.rs42
-rw-r--r--old_curriculum/functions/functions3.rs42
-rw-r--r--old_curriculum/functions/functions4.rs44
-rw-r--r--old_curriculum/functions/functions5.rs44
5 files changed, 216 insertions, 0 deletions
diff --git a/old_curriculum/functions/functions1.rs b/old_curriculum/functions/functions1.rs
new file mode 100644
index 0000000..396dd56
--- /dev/null
+++ b/old_curriculum/functions/functions1.rs
@@ -0,0 +1,44 @@
+// functions1.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me();
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This main function is calling a function that it expects to exist, but the
+// function doesn't exist. It expects this function to have the name `call_me`.
+// It expects this function to not take any arguments and not return a value.
+// Sounds a lot like `main`, doesn't it?
diff --git a/old_curriculum/functions/functions2.rs b/old_curriculum/functions/functions2.rs
new file mode 100644
index 0000000..1cf95c3
--- /dev/null
+++ b/old_curriculum/functions/functions2.rs
@@ -0,0 +1,42 @@
+// functions2.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me(3);
+}
+
+fn call_me(num) {
+ for i in 0..num {
+ println!("Ring! Call number {}", i + 1);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// Rust requires that all parts of a function's signature have type annotations,
+// but `call_me` is missing the type annotation of `num`.
diff --git a/old_curriculum/functions/functions3.rs b/old_curriculum/functions/functions3.rs
new file mode 100644
index 0000000..b17543b
--- /dev/null
+++ b/old_curriculum/functions/functions3.rs
@@ -0,0 +1,42 @@
+// functions3.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ call_me();
+}
+
+fn call_me(num: i32) {
+ for i in 0..num {
+ println!("Ring! Call number {}", i + 1);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This time, the function *declaration* is okay, but there's something wrong
+// with the place where we're calling the function.
diff --git a/old_curriculum/functions/functions4.rs b/old_curriculum/functions/functions4.rs
new file mode 100644
index 0000000..5baca0e
--- /dev/null
+++ b/old_curriculum/functions/functions4.rs
@@ -0,0 +1,44 @@
+// functions4.rs
+// Make me compile! Scroll down for hints :)
+
+// This store is having a sale where if the price is an even number, you get
+// 10 (money unit) off, but if it's an odd number, it's 3 (money unit) less.
+
+fn main() {
+ let original_price = 51;
+ println!("Your sale price is {}", sale_price(original_price));
+}
+
+fn sale_price(price: i32) -> {
+ if is_even(price) {
+ price - 10
+ } else {
+ price - 3
+ }
+}
+
+fn is_even(num: i32) -> bool {
+ num % 2 == 0
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// The error message points to line 12 and says it expects a type after the
+// `->`. This is where the function's return type should be-- take a look at
+// the `is_even` function for an example!
diff --git a/old_curriculum/functions/functions5.rs b/old_curriculum/functions/functions5.rs
new file mode 100644
index 0000000..f8fac5d
--- /dev/null
+++ b/old_curriculum/functions/functions5.rs
@@ -0,0 +1,44 @@
+// functions5.rs
+// Make me compile! Scroll down for hints :)
+
+fn main() {
+ let answer = square(3);
+ println!("The answer is {}", answer);
+}
+
+fn square(num: i32) -> i32 {
+ num * num;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+// This is a really common error that can be fixed by removing one character.
+// It happens because Rust distinguishes between expressions and statements: expressions return
+// a value and statements don't. We want to return a value from the `square` function, but it
+// isn't returning one right now...