About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function arguments and formal parameters of .
When defining a function , In parentheses is a comma separated list of formal parameters ( It's called formal parameter for short ), Pass arguments to the function when it is called , According to different parameter types , Pass the value or reference of the argument to the formal parameter .
When the parameter type is fixed data type ( Such as integers 、 Floating point numbers 、 character string 、 Tuples etc. ) when , Modifying the value of a parameter directly inside a function does not affect the actual parameter .
example : Read the following procedure , Analyze the output .
def ChangeInt(a):
a = 10
a = 2
ChangeInt(a)
print('a =',a)
give the result as follows .
But when the parameter type is variable data type ( As listing 、 Dictionaries 、 Collection etc. ) when , Add to a function by subscript or other means 、 When deleting an element or modifying an element value , The modified result can be reflected outside the function , That is, the arguments will be modified accordingly .
example : Read the following procedure , Analyze the output .
def changeme(mylist):
mylist.append([1,2,3,4])
print(" Value in function : ", mylist)
return
mylist = [10,20,30]
changeme(mylist)
print (" Value outside the function : ",mylist)
give the result as follows .
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
The above is about Python Function arguments and formal parameters , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .