Jelajahi Sumber

Update tutorials/scripting/gdscript/gdscript_basics.rst

Lambdas need an explicit `return`

Extend the documentation so that it is clear that lambdas require an explicit return .

Also add an according example.

Co-Authored-By: Danil Alexeev <[email protected]>
Mathias L. Baumann 1 tahun lalu
induk
melakukan
d3ee49e2b7
1 mengubah file dengan 6 tambahan dan 0 penghapusan
  1. 6 0
      tutorials/scripting/gdscript/gdscript_basics.rst

+ 6 - 0
tutorials/scripting/gdscript/gdscript_basics.rst

@@ -1347,6 +1347,12 @@ Lambda functions can be named for debugging purposes::
     var lambda = func my_lambda(x):
         print(x)
 
+Note that if you want to return a value from a lambda, an explicit ``return``
+is required (you can't omit ``return``)::
+
+    var lambda = func(x): return x ** 2
+    print(lambda.call(2)) # Prints `4`.
+
 Lambda functions capture the local environment. Local variables are passed by value, so they won't be updated in the lambda if changed in the local function::
 
     var x = 42