Renderingla

redde

Returns a value from a function.

Aliases: return

Syntax: redde <expression>

Category

transfer

Related

Examples

examples/corpus/redde/redde.fab (canonical · keyword)

Returns a value from a function.

# redde statements (function return)
#
# redde <expression>   -- return a value
# redde                -- return void (vacuum)
#
# GRAMMAR:
#   returnStmt :← 'redde' expr?
#
# EXPECTED OUTPUT:
#   30, Salve, Munde, 42, 0, 20

functio adde(numerus a, numerus b) → numerus {
    redde a + b
}

functio saluta(textus nomen) → textus {
    redde "Salve, " + nomen
}

functio duoetquadraginta() → numerus {
    redde 42
}

functio tace() → vacuum {
    # bare redde for vacuum return type
    redde
}

functio porta(numerus x) → numerus {
    si x < 0 {
        # early return on negative input
        redde 0
    }
    redde x * 2
}

incipit {
    nota adde(10, 20)
    nota saluta("Munde")
    nota duoetquadraginta()
    tace()
    nota porta(-5)
    nota porta(10)
}

Expected output:

30
Salve, Munde
42
0
20