Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Functions

test

// function definition
fn f() {}
// call
f();

test

// parameter
fn f(n: Scalar) {
    // result value
    n+1
}
use std::debug::*;
assert_eq([ f(1), 2 ]);
assert_eq([ f(4), 5 ]);

test

fn f(n: Scalar) {
    return n+1;
}
use std::debug::*;
assert_eq([ f(1), 2 ]);
assert_eq([ f(4), 5 ]);

test

fn f(n: Scalar) {
    if n > 3 {
        n-1
    } else {
        n+1
    }
}
use std::debug::*;
assert_eq([ f(1), 2 ]);
assert_eq([ f(4), 3 ]);

test

fn f(n: Scalar) {
    if n > 3 {
        return n-1;
    }
    n+1
}
use std::debug::*;
assert_eq([ f(1), 2 ]);
assert_eq([ f(4), 3 ]);

test

fn f(n: Scalar) { // error: not all paths return a value
    if n > 3 {
         n-1
    }
}
use std::debug::*;
assert_eq([ f(1), 2 ]);
assert_eq([ f(4), 3 ]);