Overview
Functions in b5 work like defining functions in traditional programming languages. Unlike variables that run once during setup, functions are only executed when you place them in the Playground canvas. This makes them perfect for reusable logic that needs to run repeatedly.Creating a Function
Name Your Function
Use the rename icon to give your function a descriptive name that indicates what it does.
Function names follow the same rules as variables: alphanumeric characters only, 1-16 characters, and no conflicts with existing blocks.
Define Input Parameters
Add num (number), str (string), or bool (boolean) blocks to create input parameters for your function. These appear as input nodes on your function block.
Build the Function Logic
Create the computation or drawing logic using the available blocks. The function canvas supports up to 199 lines and 19 blocks per line.
Function Block Characteristics
- Custom Inputs: Use input blocks (
num,str,bool) to define parameters - Multiple Outputs: Any block with unused output nodes creates a function output
- Execution on Demand: Only runs when placed in Playground, not during initialization
- Reusable: Can be used multiple times throughout your project
- Nestable: Can call other custom functions
Example: Boundaries Function
Here’s a real example from the Bounce Ball demo that calculates movement boundaries:How It Works
- Input: Takes the circle radius as a parameter (via
numblock) - Gets Canvas Dimensions: Uses
canvasSizeto get width and height - Calculates Boundaries:
- Lower boundary: 0 + radius
- Upper width boundary: width - radius
- Upper height boundary: height - radius
- Outputs: Three values that can be used for bounce calculations
Using the Function
Playground Usage
"source": "custom" indicates this is a user-created function, distinguishing it from "original" (built-in) and "library" (imported) blocks.
Input Parameter Types
| Block Name | Type | Description |
|---|---|---|
num | Number | Accepts numeric values |
str | String | Accepts text strings |
bool | Boolean | Accepts true/false values |
Input blocks have a single input node (to receive the parameter) and can output to multiple blocks within your function.
Function Canvas Properties
| Property | Value | Description |
|---|---|---|
| Max Lines | 199 | Maximum number of lines in the function canvas |
| Max Blocks per Line | 19 | Maximum blocks that can fit in one line |
| Execution | On Call | Only runs when used in Playground |
| Resizable | Yes | Drag the bottom bar to adjust section height |
Advanced Techniques
Multiple Inputs
Create functions with multiple parameters by adding multiple input blocks:Multiple Outputs
Any block with an unused output node automatically becomes a function output:Effect Blocks in Functions
Functions can contain effect blocks (likefill, stroke, scale) that affect the drawing context:
Best Practices
- Single Responsibility: Each function should do one thing well
- Clear Naming: Use verb-noun combinations (e.g.,
calculateSpeed,drawPattern) - Document with Comments: Add comment blocks to explain complex logic
- Test Thoroughly: Verify with different input values before extensive use
- Minimize Side Effects: Be cautious with effect blocks that change global state
Common Use Cases
Mathematical Calculations
Create functions for complex formulas used multiple times:Drawing Patterns
Encapsulate repeated drawing logic:Data Processing
Transform or validate input data:Managing Functions
Renaming
- Click the pencil icon in section controls
- Edit the name in the preview block
- Confirm by clicking outside
Deleting
Click the trash icon to remove the function. All instances in Playground will be removed.Resizing the Canvas
Drag the resize bar at the bottom of the section to adjust the working area height.Troubleshooting
Function not executing?- Ensure it’s placed in the Playground canvas (not just defined in Factory)
- Check that all required inputs are connected
- Verify wire connections between blocks
- Check that input parameters are being used correctly
- Use comment blocks to document expected values
- Ensure all required inputs are provided when called
- Check for type mismatches (number vs. string vs. boolean)
- Verify that referenced variables exist
Execution Flow
Execution (60 FPS)
Function runs every frame (or as often as Playground loops), processing inputs and generating outputs.
Next Steps
Custom Variables
Learn about creating variables for one-time setup values
Save & Load Projects
Understand how custom functions are stored in .b5.json files