Functions and Scope
Functions are one of the most important parts of XE because they show how the language handles parameters, returns, recursion, and name lookup.
Defining a function
Use the function keyword:
function add(a, b):
return a + bCall it like this:
print(add(3, 5))Parameters
Parameters are local names inside the function body.
function greet(name):
print("Hello " + name)
return 0XE checks argument count during semantic analysis, so calling a function with the wrong number of arguments is a compiler error.
Return values
Use return to send a value back to the caller.
function square(n):
return n * nCurrent rules:
returnis only valid inside functions- if a function reaches the end without an explicit
return, XE returns0
Scope model
Functions currently use local scope only.
They can access:
- their parameters
- values created inside the function body
- built-in functions
- other user-defined functions
They do not capture outer variables from the surrounding program.
This means the following program is invalid:
x = 10
function show():
print(x)x is global at the top level, but XE functions do not close over that outer binding.
Local variables vs reassignment
Inside a function, a name behaves like any other XE name:
- first assignment creates it in the current visible scope
- later assignment reuses that same variable
function counter():
total = 0
total = total + 1
return totalRecursion
Functions can call themselves:
function fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)XE supports recursion naturally because function definitions are collected before semantic checks.
No closures yet
XE does not currently support:
- closures
- nested function capture
- lexical capture of outer variables
That is an intentional current limitation of the language implementation.
No contracts or constants yet
Two features that often come up in language discussions are not in XE right now:
- there is no function contract syntax such as
requiresorensures - there is no
constbinding syntax; all bindings are currently mutable
Next steps
- Continue with Runtime Behavior and Errors
- Or inspect the runnable Examples
