Functions
Functions allow you to organize your code into reusable blocks. In Rui, you define functions using the define keyword.
Defining Functions
Create a function using the define keyword followed by the function name and parameters:
define greet(name) {
write("Hello, " + name + "!")
}Calling Functions
To use a function, call it by name and provide any required arguments:
define greet(name) {
write("Hello, " + name + "!")
}
{/* Call the function */}
greet("Alice")
greet("Bob")Functions with Multiple Parameters
Functions can accept multiple parameters:
define add(a, b) {
suppose result = a + b
write(a + " + " + b + " = " + result)
}
add(5, 3)
add(10, 7)Functions with Return Values
Functions can return values using the return keyword:
define multiply(a, b) {
return a * b
}
suppose result = multiply(4, 5)
write("4 * 5 = " + result)Functions without Parameters
Functions don't need to have parameters:
define sayHello() {
write("Hello, World!")
}
sayHello()Local Variables
Variables defined inside functions are local to that function:
define calculateArea(length, width) {
suppose area = length * width {/* This variable is local to the function */}
return area
}
suppose result = calculateArea(5, 3)
write("Area: " + result)
{/* write(area) // This would cause an error - area is not defined here */}Example: Math Functions
Here are some useful math functions:
define square(number) {
return number * number
}
define cube(number) {
return number * number * number
}
define isEven(number) {
return number % 2 == 0
}
{/* Use the functions */}
write("Square of 5: " + square(5))
write("Cube of 3: " + cube(3))
write("Is 8 even? " + isEven(8))
write("Is 7 even? " + isEven(7))Example: String Functions
Here are some string manipulation functions:
define createGreeting(name, age) {
suppose greeting = "Hello, " + name + "! You are " + age + " years old."
return greeting
}
define formatName(firstName, lastName) {
return firstName + " " + lastName
}
{/* Use the functions */}
suppose message = createGreeting("Alice", 25)
write(message)
suppose fullName = formatName("John", "Doe")
write("Full name: " + fullName)Example: Calculator Functions
Here's a simple calculator with functions:
define add(a, b) {
return a + b
}
define subtract(a, b) {
return a - b
}
define multiply(a, b) {
return a * b
}
define divide(a, b) {
if (b != 0) {
return a / b
} else {
write("Error: Cannot divide by zero!")
return 0
}
}
{/* Use the calculator */}
suppose num1 = 10
suppose num2 = 3
write("Calculator Results:")
write(num1 + " + " + num2 + " = " + add(num1, num2))
write(num1 + " - " + num2 + " = " + subtract(num1, num2))
write(num1 + " * " + num2 + " = " + multiply(num1, num2))
write(num1 + " / " + num2 + " = " + divide(num1, num2))Example: Temperature Converter
Here's a function to convert between Celsius and Fahrenheit:
define celsiusToFahrenheit(celsius) {
suppose fahrenheit = (celsius * 9 / 5) + 32
return fahrenheit
}
define fahrenheitToCelsius(fahrenheit) {
suppose celsius = (fahrenheit - 32) * 5 / 9
return celsius
}
{/* Convert temperatures */}
suppose celsius = 25
suppose fahrenheit = celsiusToFahrenheit(celsius)
write(celsius + "°C = " + fahrenheit + "°F")
suppose fahrenheit = 77
suppose celsius = fahrenheitToCelsius(fahrenheit)
write(fahrenheit + "°F = " + celsius + "°C")Recursive Functions
Functions can call themselves, which is called recursion. This is useful for solving problems that can be broken down into smaller, similar problems:
define factorial(n) {
if (n <= 1) {
return 1
} else {
return n * factorial(n - 1)
}
}
write("Factorial of 5: " + factorial(5)) // 120Here's another recursive example - calculating Fibonacci numbers:
define fibonacci(n) {
if (n <= 1) {
return n
} else {
return fibonacci(n - 1) + fibonacci(n - 2)
}
}
write("Fibonacci sequence:")
suppose i = 0
until (i < 10) {
write("F(" + i + ") = " + fibonacci(i))
suppose i = i + 1
}Important: Recursive functions need a base case (stopping condition) to prevent infinite recursion.
Function Best Practices
- Use descriptive names: Function names should clearly describe what they do
- Keep functions small: Each function should do one thing well
- Use parameters: Make functions flexible by accepting parameters
- Return values: Use return values to provide results
- Document your functions: Add comments to explain what functions do
- Handle recursion carefully: Always include a base case to prevent infinite recursion
{/* Good function example */}
define calculateCircleArea(radius) {
{/* Calculate the area of a circle given its radius */}
suppose pi = 3.14159
suppose area = pi * radius * radius
return area
}
{/* Use the function */}
suppose radius = 5
suppose area = calculateCircleArea(radius)
write("Area of circle with radius " + radius + ": " + area)Next Steps
Now that you understand functions, learn about Booleans & Logic to work with true/false values and logical operations.