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.