Functions
Functions let you create your own reusable blocks in Code Kit. They are a great way to organise your projects, avoid repeating the same blocks over and over, and achieve more complex behaviours.
How to use functions
Creating a function
- Locate the Functions category in the toolbox.
- Click 'New function' to start defining your function.
Defining your function
- Function Name: Give your function a descriptive name (e.g.,
calculateArea
). - Return Type: Specify the type of data your function will output (e.g.,
integer
,string
,boolean
, orvoid
if it doesn't return anything). - Parameters: These are inputs to your function. Add parameters as needed (e.g.,
length
,width
). - Function Body: Drag and drop blocks into the body of your function to define what it will do with the provided parameters.
Calling your function
- Find the newly created function block in the Functions category.
- Drag it into your workspace.
- Provide values for any required parameters.
Returning values (optional)
- Use the return block inside your function to send a value back when the function is called.
- The return block should match the return type you specified when defining the function.
Define function
This block defines a function.
- Any blocks placed inside the function definition block will be executed when the function is called.
- The function can have parameters, which are inputs to the function.
- The function can also return a value, which is the output of the function.
To return to the functions builder after the function has been created, right-click on the function definition block and select 'Edit function'.
Call function
This block calls a function.
- When the function is called, the blocks inside the function definition block will be executed.
- If the function has parameters, you can provide values for them when calling the function.
- If the function returns a value, you can use the value in other blocks.
Return value
This block exits the function and returns a value, or simply exits the function if no value is provided.
The return value is used as the output value of the function call block.
Function parameter
This block represents a parameter in a function definition.
- Parameters are inputs to the function.
- You can use the parameter blocks inside the function definition block to access the values provided when the function is called.
To spawn a new parameter block, right-click on the function definition block and select 'Create XYZ', where XYZ is the parameter name.