1. def is a defining function
Syntax: def function name (parameter 1, parameter 2,......, parameter n):
Function body
return statement (return a certain value)
Give an example
def hello(name): print(name+"How are you") return
2. Def can be used as a calling function: enter the value corresponding to the function name and parameter.
def hello(name): print(name+"How are you") return hello("baby")#Call the def parameter
3. Def can also be used as a parameter
1. Position parameters
def menu(appetizer,course): print("A serving of fruit:"+appetizer) print("A small noodles"+course) menu("watermelon","Chongqing small noodles")
The "watermelon" and "small face" here are passed in the position order of corresponding parameters appetizer and course, so they are called position parameters, which is also the most common parameter type.
Appendix: Why do we need the def function
In some programs, we use duplicate part of the code in many places, just like the following piece of code:
a = 1 b = 2 c = a + b d = a + b e = a + b
The above code has been reused many timesa + b
It's very troublesome to usedef
After the function, everything becomes simple:
def sum(num1,num2) : n = num1 + num2 return n a = 1 b = 2 c = num(a,b)
At this time, the value of c is still 3
Summarize
This is the article about the usage of def in Python and what def means. For more related content on the usage of def in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!