Procedures
Basic Declaration
To declare a procedure, use the following syntax:
mod example
// names of procedures are usually in snake_case
proc greet() {
std::io::println("Hello there!")
}
Procedures can also take parameters:
// names of procedures are usually in snake_case
proc greet() {
std::io::println("Hello there!")
}
mod example
// 'add' takes arguments 'x' and 'y'...
proc add(x, y) {
std::io::println(x + y) // ...and prints their sum.
}
// 'add' takes arguments 'x' and 'y'...
proc add(x, y) {
std::io::println(x + y) // ...and prints their sum.
}
Entry point
The program starts off by executing one specific procedure. This procedure is specified in the package gerap.json, and it's usually some_module::main.
Calling
Procedures can be called by writing the procedure and the parameter values in parentheses:
mod example
proc add(x, y) {
std::io::println(x + y)
}
// this is our entry point
proc main() {
add(5, 10) // call the procedure declared above - which prints '15'
}
proc add(x, y) {
std::io::println(x + y)
}
// this is our entry point
proc main() {
add(5, 10) // call the procedure declared above - which prints '15'
}
Returning
return can be used to immediately exit from a procedure and give back a value:
mod example
proc multiply(x, y) {
return x * y
}
// this is our entry point
proc main() {
std::io::println(multiply(5, 3)) // the call to 'multiply' returns 15, which is then printed
}
proc multiply(x, y) {
return x * y
}
// this is our entry point
proc main() {
std::io::println(multiply(5, 3)) // the call to 'multiply' returns 15, which is then printed
}
Expression Procedures
A procedure can be declared to directly return a value. We can use this to rewrite our multiply-procedure from above:
proc multiply(x, y) {
return x * y
}
and shorten it to:
return x * y
}
proc multiply(x, y) = x * y