Skip to main content

Default Arguments

Default arguments (also called default parameters) let you specify a fallback value for a function’s parameter when the caller doesn’t provide one. This feature makes your functions more flexible, reduces repetition, and documents common use cases right in the function signature. In EasyBite, default arguments are expressed using the to keyword—or, if you prefer a more explicit style, with set … to. Below, we’ll dive into every detail you need to understand, step by step, with plenty of examples and explanations for absolute beginners.


Table of Contents


Why Default Arguments Matter

Imagine you have a function that prints a message to the screen. Most of the time, you want it to say “Hello,” but occasionally you’ll want “Hi,” “Greetings,” or something else. Without default arguments, you’d have to write multiple versions of the function, or always pass the word “Hello” explicitly. Default arguments solve this by letting you:

  • Write less code: You don’t need overloaded versions or repeated literal values in every call.
  • Increase readability: The function signature itself documents what happens when you omit parameters.
  • Reduce errors: If you change the common default, you update it in one place, not in dozens of calls.

Conceptual Overview

A default argument is simply a parameter that has an assigned value in the function definition. If the caller provides a value for that parameter, EasyBite uses it. If the caller omits it, EasyBite “fills in” the default. You can think of the function signature as saying, “I need these two things, and if you don’t give me the second one, I’ll use this default instead.”


Syntax Variants in EasyBite

EasyBite supports two equivalent ways to define defaults. Pick the style you like:

Using to

This concise style attaches the default right to the parameter name:

function functionName(param1 to default1, param2 to default2, ...)
// function body
end function
  • param1 to default1 means: “Inside the function, if the caller didn’t pass a value for param1, treat it as default1.”

Using set … to

For those who prefer the familiar assignment keyword, you can write:

function functionName(set param1 to default1, set param2 to default2, ...)
// function body
end function
  • set param1 to default1 reads like regular variable initialization but within the signature.

Both forms compile to the same behavior; choose whichever reads best to you.


Parameter Order Rules

EasyBite requires that all required parameters (those without defaults) come before any parameters with defaults. This rule prevents confusion about which argument matches which parameter. For example:

--Correct: requiredParam first, then defaultParam
function example(requiredParam, optionalParam to 42)
...
end function

--Incorrect: defaultParam before requiredParam
function broken(defaultParam to 42, requiredParam)
...
end function

If you try the broken version, EasyBite will raise a syntax error because it cannot unambiguously map arguments.


How Defaults Are Applied at Call Time

  1. Count arguments: When you call foo(a, b), EasyBite sees two arguments.
  2. Match to parameters: It assigns them to the first two parameters in the signature.
  3. Fill in missing: If the signature has more parameters with defaults, EasyBite assigns each missing one its default value, in left‑to‑right order.
  4. Execute body: Inside the function, every parameter has a well‑defined value, whether supplied or default.

By understanding this process, you’ll avoid surprises when mixing required and default parameters.


Step‑by‑Step Example: Greeting Function

Let’s build a real greeting function from scratch, explaining every detail.

function greet(name, greeting to "Hello")
show greeting + ", " + name + "!"
end function
  1. Signature breakdown

    • name: required parameter, caller must supply.
    • greeting to "Hello": optional, defaults to "Hello".
  2. Calling with both arguments

    greet("Alice", "Hi")
    • name ← "Alice"
    • greeting ← "Hi"
    • Displays: Hi, Alice!
  3. Calling with only the required

    greet("Bob")
    • name ← "Bob"
    • greeting defaults to "Hello"
    • Displays: Hello, Bob!
  4. Why it’s beginner‑friendly

    • You see the default right in the signature.
    • You don’t have to remember a separate constant.
    • Your calls stay concise when you’re fine with the common case.

Example: Power Function with Default Exponent

Calculate base to the power of exponent, defaulting to square:

function power(base, exponent to 2)
return base ^ exponent
end function
  • power(5)5 ^ 2 = 25
  • power(3, 3)3 ^ 3 = 27
  • power(7, 1)7 ^ 1 = 7

Detailed walkthrough:

  • Call power(5). EasyBite sees one argument → base = 5.
  • Next parameter exponent has a default → exponent = 2.
  • Compute 5 ^ 2 and return 25.

Example: Recursive Factorial with Defaults

With recursion now supported, you can even combine it with defaults:

function factorial(n, accumulator to 1)
if n <= 1
return accumulator
else
-- multiply accumulator by n, then recurse
return factorial(n - 1, accumulator * n)
end if
end function
  • Calling factorial(5) runs as:
    1. n=5, accumulator=1 → recurse with n=4, accumulator=5
    2. n=4, accumulator=5 → recurse with n=3, accumulator=20
    3. … until n=1 → return 120

This tail‑recursive style is both efficient and clear. The default accumulator means you never have to think about the second parameter when you want the simple n! result.


Common Pitfalls and How to Avoid Them

  1. Swapping order: Always place required parameters first.
  2. Overriding defaults unintentionally: If you pass nil or an empty string, EasyBite treats it as a provided value, not a signal to use the default.
  3. Mutable defaults (future): If EasyBite adds lists or tables as defaults, be careful—they may be shared across calls. For now, stick to numbers, strings, or booleans.
  4. Too many defaults: If you have more than two or three defaults, consider breaking into multiple functions or using configuration objects for clarity.

Tips & Best Practices

  • Choose sensible defaults that cover 80% of your use cases.
  • Document each default in comments above the function to explain “why this value?”
  • Keep function signatures short: more than four parameters (including defaults) can confuse callers.
  • Use the explicit set … to style when you want the signature to read like variable initialization.
  • Write examples in your code comments showing both the default‑omitting call and the override call.

Conclusion

Default arguments in EasyBite empower you to write cleaner, more expressive functions. By using the simple to syntax (or set … to if you prefer), you can:

  • Omit common values in your calls.
  • Document behavior right in the signature.
  • Combine seamlessly with recursion or loops for powerful patterns.