Calling Functions
Calling a function is how you execute the block of code you defined with function … end function
. In EasyBite, function calls look like small statements: you write the function’s name, give it the inputs (arguments), and optionally capture its output. This guide will walk you through every detail—even if you’ve never called a function before.
Table of Contents
Why Function Calls Matter
- Reuse your logic: Once you write a function, you can call it as many times as you like without rewriting its body.
- Keep code tidy: Function calls abstract away details—readers see just the name and arguments.
- Chain behaviors: You can feed the output of one function into another, creating powerful pipelines.
Understanding how to call functions is your first step to modular, maintainable EasyBite code.
Basic Call Syntax
The simplest function call is just the function name plus parentheses:
function sayHello()
show "Hello, world!"
end function
// Call it:
sayHello()
sayHello()
tells EasyBite: “Run the code in the function calledsayHello
.”- Even with no parameters, you still include the
()
.
Passing Arguments
Most functions need data to work with—these are arguments. You place them inside the parentheses, separated by commas.
Required Arguments
If your function signature lists parameters without defaults, you must pass values in the same order:
function add(a, b)
return a + b
end function
// Correct:
set sum to add(4, 7) // sum = 11
// ❌ Incorrect (missing one argument):
add(4) // Error: b is undefined
Default Arguments
If a function parameter has a default, you can omit that argument. EasyBite will fill in the default for you:
function greet(name, greeting to "Hello")
show greeting + ", " + name + "!"
end function
greet("Alice") // Uses default → "Hello, Alice!"
greet("Bob", "Hi") // Overrides default → "Hi, Bob!"
Storing Return Values
When a function uses return
, it hands back a value. To use that value later, you capture it in a variable with set … to
:
function multiply(x, y)
return x * y
end function
set result to multiply(5, 3)
show "5 × 3 = " + result // Displays: 5 × 3 = 15
set result to multiply(5, 3)
runsmultiply
, then stores its return inresult
.- You can then use
result
elsewhere in your code.
Calling Procedures (No Return Value)
Some functions perform actions (like showing a message) but don’t return
anything. These are sometimes called procedures:
function logMessage(msg)
show "[LOG] " + msg
end function
logMessage("Data saved.") // Displays: [LOG] Data saved.
- You call them the same way, but you don’t assign them to a variable, because there’s nothing to capture.
Nested Calls
You can call a function inside another call or expression. EasyBite evaluates the innermost call first:
function square(n)
return n ^ 2
end function
function sumOfSquares(a, b)
return square(a) + square(b)
end function
set total to sumOfSquares(2, 3)
// Evaluates: square(2)=4, square(3)=9, then 4+9=13
show total // Displays: 13
sumOfSquares(2, 3)
callssquare(2)
andsquare(3)
before adding.
Recursion Calls
With recursion support, a function can call itself. Calling a recursive function looks the same as any other call:
function factorial(n)
if n <= 1
return 1
else
return n * factorial(n - 1)
end if
end function
set fiveFact to factorial(5)
// factorial(5) → 5 × factorial(4)
// factorial(4) → 4 × factorial(3)
// … until factorial(1) → returns 1
// Result: 120
show "5! = " + fiveFact // Displays: 5! = 120
- Each call to
factorial
gets its own copy ofn
. - EasyBite handles the call stack automatically.
Common Mistakes & How to Fix Them
Mistake | Symptom | Fix |
---|---|---|
Omitting parentheses on a call | Nothing happens | Always write functionName() , even if there are no parameters. |
Passing arguments in wrong order | Wrong result or type error | Match the exact order from the function signature. |
Forgetting set … to for return values | Value lost, can’t use the output | Use set var to functionName(args) when you need the return. |
Overwriting a variable with a function name | Unexpected behavior or error | Use unique variable names, don’t reuse function names. |
Mixing required and default arguments wrongly | Missing values or extra defaults | Put all required parameters first, then defaults. |
Tips & Best Practices
- Name your functions clearly:
calculateArea
, not justarea
. - Use descriptive argument names: In
add(x, y)
, consideradd(width, height)
for clarity. - Capture returns immediately if you need them:
set result to …
. - Log or
show
intermediate results when debugging nested or recursive calls. - Don’t overnest: if you have more than two levels of nesting, consider splitting into intermediate variables for readability.
Conclusion
Calling functions is the bridge between defining behavior and executing it. In EasyBite:
- Use
functionName(args…)
to invoke. - Pass required arguments in order, omit defaults when desired.
- Capture outputs with
set … to
whenreturn
is used. - Parentheses
()
are always required.
Practice by writing small functions (greetings, math operations, string handlers) and call them in various ways—mix arguments, nest calls, even try simple recursion. With these skills, you’ll be confidently orchestrating your EasyBite programs, one function call at a time!