Neon
← Back to LoopsNext: Handling errors →

Function Definitions

Functions allow encapsulating reusable code. Neon supports:


1. Defining a Procedure

A procedure is a function with no arguments that returns None.

Syntax

function functionName() do
    # Code to execute
end

Example

function greet() do
    print("Hello, Neon!")
end

# Call the procedure
greet()  # → Prints "Hello, Neon!"

2. Defining a Function with Arguments

Functions can take arguments and return a value using return().

Syntax

function functionName(arg1, arg2, ...) do
    # Code
    return (expression)  # Optional
end

Examples

# 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)

3. Local and Global Variables

How Local Variables Work

Examples

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)

4. Methods

A method is like a function, but it can modify its first argument.

Syntax

method methodName(arg1, arg2, ...) do
    # arg1 can be modified directly
    # Changes to arg1 will affect the original object
end

Example

# 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)

5. Advanced Argument Passing


a) Named Arguments (Out-of-Order)

You can pass arguments by name using :=.

Example

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

b) Optional Arguments

Arguments can have default values using :=.

Syntax

function functionName(requiredArg, optionalArg := defaultValue) do
    ...
end

Example

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

c) Variadic Arguments (Unlimited Arguments)

Use ... to accept any number of arguments. Extra arguments are stored in __local_args__.

Syntax

function functionName(arg1, arg2, ...) do
    # __local_args__ is a list of extra arguments
    ...
end

Example

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

d) Truly Optional Arguments

After ..., you can define optional arguments that must be passed by name.

Example

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_=[]

6. Higher-Order Functions (Closures Simulation)

Neon does not natively support closures, but you can simulate them using optional arguments.

Example: Adder Function

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

How It Works


7. Function Documentation

Use setFunctionDoc to add help text to your functions.

Example

function multiply(a, b) do
    return (a * b)
end

setFunctionDoc(multiply, "Multiplies two numbers and returns the result.")
help(multiply)  # Shows the docstring

← Back to LoopsNext: Handling errors →