RustArena

Step 1 of 6

Defining and calling functions

You've been writing one function all along: main. A program can have as many functions as you like. A function is a named block of code that you can run, or call, whenever you need it, by writing its name followed by ().

fn main() {
    println!("Match starting");
    print_divider();
    println!("Round 1");
    print_divider();
}

fn print_divider() {
    println!("----------");
}
Match starting
----------
Round 1
----------

Why write functions?

Functions can call functions

Any function can call any other. When a call finishes, the program returns to exactly where it was:

fn main() {
    start_round();
    start_round();
}

fn start_round() {
    println!("Fight!");
    cheer();
}

fn cheer() {
    println!("The crowd cheers");
}
Fight!
The crowd cheers
Fight!
The crowd cheers

Common mistakes

Your task

Fill in the blanks: define a function called cheer and call it twice from main, so the program prints Go, Ferris! two times.

Challenge 2

Fill in the blanks so main calls intro, and intro calls rules. The program should print:

Welcome to the arena
Rule 1: no sandals

Challenge 3

Write a function print_banner that prints these three lines, and call it twice from main (six lines in total):

+--------+
| ARENA  |
+--------+

Next: Parameters