函数
本文档说明了函数,详细说明了如何返回值和调用函数。
返回值
返回
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"function_return\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" no_return_value=\"false\"></mutation><value name=\"RETURN_VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow></value></block></xml>"}
此块用于返回函数内部的值。
参数: 屏幕项目
输出: N/A
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>mainLayout</variable><variable>mainScreen</variable><variable>dateTimeOffset</variable></variables><block type=\"function_definition\" x=\"20\" y=\"305\"><mutation name=\"calculation\" functionid=\"O$xs+CBy0Ty=@dN;0n7q\"><arg name=\"num\" id=\"b1x9b17oeik0sacu18ayb\" type=\"number\"/><arg name=\"num2\" id=\"pa6vzne4zkvawshxz5w6j\" type=\"number\"/></mutation><field name=\"function_name\">calculation</field><value name=\"b1x9b17oeik0sacu18ayb\"><shadow type=\"argument_reporter_number\"><field name=\"VALUE\">num</field></shadow></value><value name=\"pa6vzne4zkvawshxz5w6j\"><shadow type=\"argument_reporter_number\"><field name=\"VALUE\">num2</field></shadow></value><statement name=\"STACK\"><block type=\"function_return\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" no_return_value=\"false\"></mutation><value name=\"RETURN_VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MULTIPLY</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"argument_reporter_number\"><field name=\"VALUE\">num</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"argument_reporter_number\"><field name=\"VALUE\">num2</field></block></value></block></value></block></statement></block><block type=\"pxt-on-start\" x=\"20\" y=\"20\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"function_call_output\"><mutation name=\"calculation\" functionid=\"O$xs+CBy0Ty=@dN;0n7q\"><arg name=\"num\" id=\"b1x9b17oeik0sacu18ayb\" type=\"number\"/><arg name=\"num2\" id=\"pa6vzne4zkvawshxz5w6j\" type=\"number\"/></mutation><value name=\"b1x9b17oeik0sacu18ayb\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value><value name=\"pa6vzne4zkvawshxz5w6j\"><shadow type=\"math_number\"><field name=\"NUM\">1</field></shadow></value></block></value></block></value></block></value></block></next></block></next></block></statement></block></xml>"}
输出:
调用函数
叫。。。
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><block type=\"function_call\"><mutation name=\"doSomething\" functionid=\"null\"/></block></xml>"}
此块用于调用已创建的函数(名为 doSomething)。
参数: 屏幕项目
输出: N/A
示例代码:
{"blocks":"<xml xmlns=\"https://developers.google.com/blockly/xml\"><variables><variable>number</variable><variable>mainLayout</variable><variable>mainScreen</variable><variable>dateTimeOffset</variable></variables><block type=\"function_definition\" x=\"0\" y=\"285\"><mutation name=\"calculation\" functionid=\"h1nwY;~-pRcnI]Q0^m)#\"><arg name=\"num\" id=\"gfoblss38n7o0ujrl0w2g\" type=\"number\"/><arg name=\"num2\" id=\"0bz7crm1t73tdnn9qey\" type=\"number\"/></mutation><field name=\"function_name\">calculation</field><value name=\"gfoblss38n7o0ujrl0w2g\"><shadow type=\"argument_reporter_number\"><field name=\"VALUE\">num</field></shadow></value><value name=\"0bz7crm1t73tdnn9qey\"><shadow type=\"argument_reporter_number\"><field name=\"VALUE\">num2</field></shadow></value><statement name=\"STACK\"><block type=\"variables_set\"><field name=\"VAR\">number</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"math_arithmetic\"><field name=\"OP\">MULTIPLY</field><value name=\"A\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"argument_reporter_number\"><field name=\"VALUE\">num</field></block></value><value name=\"B\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"argument_reporter_number\"><field name=\"VALUE\">num2</field></block></value></block></value><next><block type=\"ScreenItem_Layout_add\"><value name=\"this\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value><value name=\"item\"><block type=\"Control_createTextBlock\"><mutation xmlns=\"http://www.w3.org/1999/xhtml\" _expanded=\"1\" _input_init=\"true\"></mutation><value name=\"text\"><block type=\"DateTime_DateTimeOffset_toString\"><value name=\"dateTimeOffset\"><block type=\"variables_get\"><field name=\"VAR\">number</field></block></value></block></value></block></value></block></next></block></statement></block><block type=\"pxt-on-start\" x=\"0\" y=\"0\"><statement name=\"HANDLER\"><block type=\"variables_set\"><field name=\"VAR\">mainLayout</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"ScreenItem_createLayout\"/></value><next><block type=\"variables_set\"><field name=\"VAR\">mainScreen</field><value name=\"VALUE\"><shadow type=\"math_number\"><field name=\"NUM\">0</field></shadow><block type=\"Screen_create\"><value name=\"label\"><shadow type=\"text\"><field name=\"TEXT\">Main</field></shadow></value><value name=\"layout\"><block type=\"variables_get\"><field name=\"VAR\">mainLayout</field></block></value></block></value><next><block type=\"function_call\"><mutation name=\"calculation\" functionid=\"h1nwY;~-pRcnI]Q0^m)#\"><arg name=\"num\" id=\"gfoblss38n7o0ujrl0w2g\" type=\"number\"/><arg name=\"num2\" id=\"0bz7crm1t73tdnn9qey\" type=\"number\"/></mutation><value name=\"gfoblss38n7o0ujrl0w2g\"><shadow type=\"math_number\"><field name=\"NUM\">5</field></shadow></value><value name=\"0bz7crm1t73tdnn9qey\"><shadow type=\"math_number\"><field name=\"NUM\">6</field></shadow></value></block></next></block></next></block></statement></block></xml>"}
输出:
目录