Skip to main content

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 called sayHello.”
  • 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) runs multiply, then stores its return in result.
  • 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) calls square(2) and square(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 of n.
  • EasyBite handles the call stack automatically.

Common Mistakes & How to Fix Them

MistakeSymptomFix
Omitting parentheses on a callNothing happensAlways write functionName(), even if there are no parameters.
Passing arguments in wrong orderWrong result or type errorMatch the exact order from the function signature.
Forgetting set … to for return valuesValue lost, can’t use the outputUse set var to functionName(args) when you need the return.
Overwriting a variable with a function nameUnexpected behavior or errorUse unique variable names, don’t reuse function names.
Mixing required and default arguments wronglyMissing values or extra defaultsPut all required parameters first, then defaults.

Tips & Best Practices

  • Name your functions clearly: calculateArea, not just area.
  • Use descriptive argument names: In add(x, y), consider add(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 when return 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!