Defining and Calling Functions
Functions are fundamental building blocks for writing clean, reusable
code. In Ry, you can define a function using the func
keyword, followed by the function name and a block of code.
# Define a simple function
func sayHello() {
out("Hello from a function!")
}
# Call the function
sayHello()
Parameters
You can pass data into functions through parameters. Parameters are
declared within the parentheses after the function name, using the
same data keyword as variables.
func greet(data name) {
out("Hello, ${name}!")
}
greet("Bob") # Output: Hello, Bob!
greet("Charlie") # Output: Hello, Charlie!
Return Values
Functions can process data and return a result using the
return keyword. When a return statement is
executed, the function immediately exits and passes the specified
value back to where it was called.
func add(data a, data b) {
return a + b
}
data sum = add(5, 3)
out(sum) # Output: 8
Functions in Ry are first-class citizens, which means they can be passed around like any other data. This enables powerful patterns like Closures.
Next: Closures!