Browse Source

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 year ago
parent
commit
d3ee49e2b7
1 changed files with 6 additions and 0 deletions
  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):
     var lambda = func my_lambda(x):
         print(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::
 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
     var x = 42