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
- Default Arguments
- Table of Contents
- Why Default Arguments Matter
- Conceptual Overview
- Syntax Variants in EasyBite
- Parameter Order Rules
- How Defaults Are Applied at Call Time
- Step‑by‑Step Example: Greeting Function
- Example: Power Function with Default Exponent
- Example: Recursive Factorial with Defaults
- Common Pitfalls and How to Avoid Them
- Tips & Best Practices
- Conclusion
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 forparam1
, treat it asdefault1
.”
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
- Count arguments: When you call
foo(a, b)
, EasyBite sees two arguments. - Match to parameters: It assigns them to the first two parameters in the signature.
- Fill in missing: If the signature has more parameters with defaults, EasyBite assigns each missing one its default value, in left‑to‑right order.
- 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
-
Signature breakdown
name
: required parameter, caller must supply.greeting to "Hello"
: optional, defaults to"Hello"
.
-
Calling with both arguments
greet("Alice", "Hi")
name
← "Alice"greeting
← "Hi"- Displays: Hi, Alice!
-
Calling with only the required
greet("Bob")
name
← "Bob"greeting
defaults to"Hello"
- Displays: Hello, Bob!
-
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 return25
.
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:n=5, accumulator=1
→ recurse withn=4, accumulator=5
n=4, accumulator=5
→ recurse withn=3, accumulator=20
- … until
n=1
→ return120
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
- Swapping order: Always place required parameters first.
- 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. - 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.
- 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.