Anonymous functions are a way of defining functions that do not need to be named , With lambda Keyword start
lambda x:x*x
def getresult(x);
return x*x
In the above code block lambda Of x Corresponding to the following getresult In parentheses x,lambda Medium x*x Corresponding return hinder x*x
An anonymous function is called an anonymous function because it has no function name . Another example
# A function with only one parameter
square=lambda x:x*x
result=square(12)
print(result)
# Functions with multiple arguments
triangle=lambda x,y:0.5*x*y
result=triangle(23,4)
print(result)
lambda The subject of is an expression , Not a block of code , It is not suitable for dealing with complex logical situations .
When we create a function that contains more complex logic , It is recommended to use def Create a function .
The internal calling function of the function itself , This self calling function is a recursive function
def sun(n)
if n<=0:
return 0
return n+sum(n-1)
print(sum(5))
The above recursive function is used to calculate the accumulation of numbers ( Calculation 5 Number accumulation within )
In recursive functions ,if Judgment is the termination condition , In the above example , When n<=0 when , Just go back to 0, Recursion ends
If there are no conditions , It's a dead circle .