Functions
In OneVision Scripts, functions allow you to encapsulate a set of instructions into a reusable block of code. Functions are essential for organizing code and reusing logic. They help in breaking down complex operations into smaller, manageable tasks.
Defining a function
A function in OneVision Scripts is defined using the function keyword followed by the function name and a pair of parentheses () containing any parameters. Then the return type (if any) is written after a colon :. The function body is enclosed within curly braces {}.
function functionName(parameter1: type, parameter2: type): returnType {
// Function body
}
For example:
function add(a: number, b: number): number {
return a + b;
}
Function parameters and return types
Functions can take parameters and return a value. You must specify the type of each parameter and the return type of the function in the function definition.
For example:
function multiply(x: number, y: number): number {
return x * y;
}
let result = multiply(4, 5); // result will be 20
console.log(result);
Calling a function
To call a function, use the function name followed by parentheses () containing any arguments. If the function returns a value, you can assign this value to a variable.
functionName(argument1, argument2);
For example:
sum = add(5, 3); // sum will be 8
console.log(sum);