Stay organized with collections
Save and categorize content based on your preferences.
blockly > CodeGenerator > provideFunction_
CodeGenerator.provideFunction_() method
Define a developer-defined function (not a user-defined procedure) to be included in the generated code. Used for creating private helper functions. The first time this is called with a given desiredName, the code is saved and an actual name is generated. Subsequent calls with the same desiredName have no effect but have the same return value.
It is up to the caller to make sure the same desiredName is not used for different helper functions (e.g. use "colourRandom" and "listRandom", not "random"). There is no danger of colliding with reserved words, or user-defined variable or procedure names.
The code gets output when CodeGenerator.finish() is called.
Signature:
provideFunction_(desiredName: string, code: string[] | string): string;
Parameters
Parameter |
Type |
Description |
desiredName |
string |
The desired name of the function (e.g. mathIsPrime). |
code |
string[] | string |
A list of statements or one multi-line code string. Use ' ' for indents (they will be replaced). |
Returns:
string
The actual name of the new function. This may differ from desiredName if the former has already been taken by the user.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["`CodeGenerator.provideFunction_()` defines developer-defined helper functions for inclusion in the generated code, assigning a unique name to avoid conflicts."],["Calling this method multiple times with the same `desiredName` has no effect but returns the previously assigned unique name."],["Generated helper function code is output when `CodeGenerator.finish()` is called."],["The method takes the desired function name and code as input, returning the actual assigned function name."],["Ensure distinct `desiredName` values for different helper functions to prevent unintended behavior."]]],["The `provideFunction_()` method defines developer-created helper functions for generated code. It takes a `desiredName` and `code` (string or string array). The first call with a `desiredName` saves the `code` and generates a function name. Subsequent calls with the same `desiredName` are ignored. The function name is returned, and may differ from `desiredName` if a name conflict is detected. The code is output when `CodeGenerator.finish()` is called. The method is used to prevent name collisions with user-defined items.\n"]]