Skip to main content

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​

  1. Locate the Functions category in the toolbox.
  2. 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, or void 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​

  1. Find the newly created function block in the Functions category.
  2. Drag it into your workspace.
  3. 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, click the cog icon on the function definition block or right-click on the function definition block and select 'Edit function'.

Define function with parameters​

When a function has parameters, they will be displayed in the function block header. These parameter blocks can be dragged into blocks used inside the function body.

In this example, a function called calculate area is defined. It takes two parameters: length and width. The function returns the product of these two parameters.

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.