NeonFunctions allow encapsulating reusable code. Neon supports:
A procedure is a function with no arguments that returns None.
function functionName() do
# Code to execute
end
function greet() do
print("Hello, Neon!")
end
# Call the procedure
greet() # → Prints "Hello, Neon!"
Functions can take arguments and return a value using return().
function functionName(arg1, arg2, ...) do
# Code
return (expression) # Optional
end
# Add two numbers
function add(a, b) do
return (a + b)
end
result = add(2, 3) # → 5
# Square a number
function square(x) do
return (x ** 2)
end
print(square(4)) # → 16
# No return (returns None)
function sayHello(name) do
print("Hello, " + name + "!")
end
sayHello("Alice") # → Prints "Hello, Alice!" (returns None)
local() to explicitly make variables local in a block.x = 10 # Global x
function modifyX() do
x = 20 # Modifies the global x!
end
modifyX()
print(x) # → 20
# To avoid modifying global x:
function safeModifyX() do
local(x) # Makes x local to this function
x = 30 # Does not affect global x
print(x) # → 30
end
safeModifyX()
print(x) # → 20 (unchanged)
# Local in loops
i = 0
for (i, 5) do
print(i) # i is local to the loop
end
print(i) # → 0 (restored to original value)
A method is like a function, but it can modify its first argument.
method methodName(arg1, arg2, ...) do
# arg1 can be modified directly
# Changes to arg1 will affect the original object
end
# Increment a number (modifies the original)
method increment(x) do
x += 1
end
n = 5
increment(n)
print(n) # → 6 (modified!)
# Compare with a function (does not modify original)
function tryIncrement(x) do
x += 1
end
m = 5
tryIncrement(m)
print(m) # → 5 (unchanged)
You can pass arguments by name using :=.
function createPerson(name, age, city) do
print("Name: " + name + ", Age: " + str(age) + ", City: " + city)
end
# Normal call
createPerson("Alice", 30, "Paris")
# Named arguments (order doesn't matter)
createPerson(city := "Berlin", name := "Bob", age := 25)
createPerson("Charlie", city := "London", age := 40) # Mix named and positional
Arguments can have default values using :=.
function functionName(requiredArg, optionalArg := defaultValue) do
...
end
function greet(name, greeting := "Hello") do
print(greeting + ", " + name + "!")
end
greet("Alice") # → "Hello, Alice!"
greet("Bob", "Hi") # → "Hi, Bob!"
greet(name := "Charlie", greeting := "Hey") # Named arguments
Use ... to accept any number of arguments. Extra arguments are stored in __local_args__.
function functionName(arg1, arg2, ...) do
# __local_args__ is a list of extra arguments
...
end
function sumAll(a, b, ...) do
total = a + b
foreach (num, _local_args_) do
total += num
end
return (total)
end
print(sumAll(1, 2)) # → 3
print(sumAll(1, 2, 3)) # → 6
print(sumAll(1, 2, 3, 4)) # → 10
After ..., you can define optional arguments that must be passed by name.
function example(a, b, ..., c := 10) do
print("a: " + str(a) + ", b: " + str(b) + ", c: " + str(c))
print("_local_args_: " + str(_local_args_))
end
example(1, 2) # a=1, b=2, c=10, _local_args_=[]
example(1, 2, 3) # a=1, b=2, c=10, _local_args_=[3]
example(1, 2, c := 20) # a=1, b=2, c=20, _local_args_=[]
Neon does not natively support closures, but you can simulate them using optional arguments.
function makeAdder(addValue) do
function adder(x, addValue := addValue) do
return (x + addValue)
end
return (adder)
end
add5 = makeAdder(5) # Returns a function that adds 5
print(add5(10)) # → 15
print(add5(20)) # → 25
add10 = makeAdder(10)
print(add10(3)) # → 13
adder function captures addValue as a default argument.makeAdder(5) is called, addValue is set to 5 in the function definition.makeAdder creates a new function with its own addValue.Use setFunctionDoc to add help text to your functions.
function multiply(a, b) do
return (a * b)
end
setFunctionDoc(multiply, "Multiplies two numbers and returns the result.")
help(multiply) # Shows the docstring