Skip to main content

rl_lang/docs/entries/tutorial/t1/
p8.rs

1use crate::docs::entry::{ConceptCategory, ConceptEntry, DescriptionEntry, DescriptionKind};
2pub static STEP_FUNCTIONS: ConceptEntry = ConceptEntry {
3    name: "8. grouping logic into functions",
4    summary: "grouping logic into functions",
5    category: ConceptCategory::Functions,
6    prerequisites: &[],
7    descriptions: &[
8        DescriptionEntry {
9            kind: DescriptionKind::Explanation,
10            title: None,
11            description: "a function is a named block of code you can call by name. instead of copying the same lines everywhere you write them once as a function and call it wherever you need it",
12            examples: &[
13                "fn greet(string name) {\n    println(\"hello \")\n    println(name)\n}\n\ngreet(\"Ali\")\ngreet(\"Sara\")",
14            ],
15            expected_output: &[],
16        },
17        DescriptionEntry {
18            kind: DescriptionKind::Explanation,
19            title: None,
20            description: "functions can give back a value with return. you declare what type they return with -> after the parameter list",
21            examples: &[
22                "fn add(int a, int b) -> int {\n    return a + b\n}\n\ndec int res = add(3, 4)\nprintln(result) // 7",
23            ],
24            expected_output: &[],
25        },
26        DescriptionEntry {
27            kind: DescriptionKind::Explanation,
28            title: None,
29            description: "functions can call themselves, this is called recursion. each call works on a smaller version of the problem until it hits a base case that stops the recursion",
30            examples: &[
31                "fn countdown(int n) {\n    if (n == 0) {\n        println(\"go!\")\n        return\n    }\n    println(n)\n    countdown(n - 1)\n}\n\ncountdown(3)\n// 3\n// 2\n// 1\n// go!",
32            ],
33            expected_output: &[],
34        },
35        DescriptionEntry {
36            kind: DescriptionKind::Explanation,
37            title: None,
38            description: "exercise: pull the hint logic and the divider printer out of your game loop into their own functions. your main game loop should just call them\n\nwrite:\n  fn print_divider() - prints a row of dashes\n  fn check_guess(int guess, int secret) -> string - returns \"low\", \"high\", or \"correct\"",
39            examples: &[
40                "get print   from std::io\nget concat  from std::str\n\nfn print_divider() {\n    for i in 0..20 {\n        print(\"-\")\n    }\n    println(\"\")\n}\n\nfn check_guess(int guess, int secret) -> string {\n    if (guess < secret) { return \"low\" }\n    if (guess > secret) { return \"high\" }\n    return \"correct\"\n}\n\n// in your game loop:\n// dec string result = check_guess(guess, secret)\n// if (result == \"correct\") { ... }",
41            ],
42            expected_output: &[],
43        },
44    ],
45    pitfalls: &[],
46    related: &[],
47    related_stdlib: &[],
48    since: None,
49};