assistance-engine/ingestion/docs/23_Function_architecture_sc...

99 lines
2.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

SECTION VII: Function Architecture and Scopes
This section explains how to encapsulate reusable logic and how AVAP manages isolated memory to prevent side effects across different parts of the program.
7.1 Definition and Declaration (function)
A function in AVAP is an independent block of code registered in the engine so it can be invoked at any time.
Interface
function function_name(argument1, argument2, ...){ ... }
Technical Characteristics
Local Scope (function_local_vars):
When entering a function, AVAP creates a new local variable dictionary. Variables created inside the function (e.g., temp = 10) do not exist outside of it, protecting the global state.
Context Inheritance:
Functions can read global variables using the $ prefix, but any new assignment (=) remains in the local scope unless an explicit global persistence command is used.
7.2 The Return Command (return)
This is the mechanism used to terminate function execution and optionally send a value back to the caller.
Interface
return(variable_or_value)
Behavior
Termination:
Immediately stops function processing.
Data Transfer:
The value passed to return is injected into the variable that invoked the function in the main flow.
Cleanup:
Once return executes, the functions local variable dictionary is destroyed to free memory.
7.3 Invocation and Parameter Passing
Functions are called by name followed by the required values or variables.
Professional Implementation Example
// Function definition (Local Scope)
function calculate_discount(base_price, percentage){
factor = percentage / 100
discount = base_price * factor
total = base_price - discount
return(total)
}
// Main Flow (Global Scope)
addVar(pvp, 150)
// Function call passing a reference $ and a literal value
final_price = calculate_discount($pvp, 20)
addResult(final_price) // Result: 120
7.4 Functions as Middlewares
In the registerEndpoint command (Section I), the middleware parameter accepts a list of functions. These functions have special behavior:
If a middleware executes a return() without a value or with an error value, AVAP can be configured to abort the request before reaching the main handler.
Ideal for guard tasks such as:
API key verification
Data schema validation
Initial audit logging
7.5 Recursion and Limits
AVAP supports recursion (a function calling itself), but caution is recommended regarding stack depth, especially in asynchronous processes (Section IV).
For processing large volumes of data, it is always preferable to use startLoop (Section III) instead of deep recursive calls.
Examples
1. Modular Sum Function
Code snippet
function sum(a, b){
total = a + b
return(total)
}
result = sum(10, 20)
2. Access Validation Function
Code snippet
function is_valid(token){
if(token, "SECRET", "==")
return(True)
end()
return(False)
}
authorized = is_valid($input_token)