当前位置:网站首页>XV Function definition and call
XV Function definition and call
2022-07-06 12:57:00 【Name it casually】
Catalog
3、 ... and . Function creation
Four . Parameter passing for function calls
5、 ... and . Memory analysis of function calls
6、 ... and .return When the function returns multiple values , Result is tuple
7、 ... and . The return value of the function
8、 ... and . Parameter definition of function
One . What is a function
· A function is a piece of code that performs a specific task to complete a specific function
Two . Why function is needed
· Code reuse
· Hide implementation details
· Improve maintainability
· Improve readability, easy to debug
3、 ... and . Function creation
def Function name ( Input parameters ):
The body of the function
return xxx
Function creation :
def sum(a,b):
c =a+b
return print(c)
Function call :
sum(10,20)
Print :
30
-----------------------------------------------
def sum(a,b):
c =a+b
return c
print(sum(10,20))
Four . Parameter passing for function calls
- Location parameter
- The argument is passed according to the position corresponding to the formal parameter
def calc(a,b):
calc(10,20) #10 Corresponding a,20 Corresponding b
- Keyword arguments
- Pass arguments according to the formal parameter name
def calc(a,b):
calc(b=10,a=20)
5、 ... and . Memory analysis of function calls
''' During a function call , Pass parameters If it's an immutable object , Changes in the function body do not affect the value of the argument If it's a mutable object , Changes in the function body will affect the value of the argument '''
n1=11
n2=[22,33,44]
print('n1:',n1)
print('n2:',n2)
print('------------------------------------------------')
def fun(arg1,arg2):
print('arg1:',arg1)
print('arg2:',arg2)
arg1=100
arg2.append(10)
print('arg1:',arg1)
print('arg2:',arg2)
fun(n1,n2)
''' During a function call , Pass parameters
If it's an immutable object , Changes in the function body do not affect the value of the argument
If it's a mutable object , Changes in the function body will affect the value of the argument
'''
print('-------------------------------------------------')
print('n1',n1)
print('n2',n2)
Print :
n1: 11
n2: [22, 33, 44]
------------------------------------------------
arg1: 11
arg2: [22, 33, 44]
arg1: 100
arg2: [22, 33, 44, 10]
-------------------------------------------------
n1 11
n2 [22, 33, 44, 10]
6、 ... and .return When the function returns multiple values , Result is tuple
def fun2(num):
odd=[]
even=[]
for i in num:
if i % 2:
odd.append(i)
else:
even.append(i)
return odd,even
lis=[1,2,3,4,5]
print(fun2(lis))
Print :
([1, 3, 5], [2, 4])
---------------------------------------------
def fun2(num):
odd=[]
even=[]
for i in num:
if i % 2:
odd.append(i)
else:
even.append(i)
return odd
lis=[1,2,3,4,5]
print(fun2(lis))
Print :
[1, 3, 5]
7、 ... and . The return value of the function
- If the function does not return a value 【 After the function is executed , There is no need to provide data to the caller 】return You can omit it
- The return value of the function , If it is 1 individual , Direct return type
- The return value of the function , If more than one , The returned result is a tuple
- When a function is defined , Whether to return a value , As the case may be
def fun1():
print("hello")
fun1()
def fun2():
return 'hello,word'
print(fun2())
def fun3():
return 'hell','word'
print(fun3())
Print :
hello
hello,word
('hell', 'word')
8、 ... and . Parameter definition of function
1. Function defines default value parameters
- When a function is defined , Set default values for parameters , Arguments need to be passed only if they do not match the default values
def fun1(a,b=10):
print(a,b)
fun1(30)
fun1(30,20)
Print :
30 10
30 20
2. Variable number of position parameters *
- When defining a function , When the number of location arguments passed may not be determined in advance , Use variable position parameters
- Use * Define a variable number of positional parameters
- The result is a tuple
- When there are multiple parameters , Only the last parameter can be defined *
- * and ** When used at the same time , Need to be * before ** After
- Each function can only have 1 individual
def fun1(*a):
print(a)
fun1(10,220,30)
Print :
(10, 220, 30)
----------------------------
def fun1(a,*b):
print(a,b)
fun1(10,220,30)
Print :
10 (220, 30)
- Reversible call , Turn the elements in the list into arguments and pass them in *list
- The elements in the list need to correspond to the number of parameters in the function
def fun1(a,b,c):
print(a,b,c)
lis=[10,20,30]
fun1(*lis)
Print :
10 20 30
3. Variable number of keyword parameters **
- When defining a function , When the number of keyword arguments passed cannot be determined in advance , Use variable keyword parameters
- Use ** Define a variable number of keyword parameters
- The result is a dictionary
- When there are multiple parameters , Only the last parameter can be defined **
- * and ** When used at the same time , Need to be * before ** After
- Each function can only have 1 individual
def fun1(**a):
print(a)
fun1( Xiao Ming =10, petty thief =20)
Print :
{' Xiao Ming ': 10, ' petty thief ': 20}
-----------------------------------
def fun1(a,**b):
print(a,b)
fun1(10, Xiao Ming =10, petty thief =20)
Print :
10 {' Xiao Ming ': 10, ' petty thief ': 20}
------------------------------------
def fun1(*a,**b): #* and ** When used at the same time , Need to be * before ** After
pass
def fun1(**a,*b): # Report errors
pass
- Reversible call , Turn the elements in the list into arguments and pass them in **dict
- The elements in the list need to correspond to the number of parameters in the function
- key The value needs to be consistent with the parameter name in the function
def fun1(a,b,c):
print(a,b,c)
dic={"a":50,"b":20,"c":60}
fun1(**dic)
Print :
50 20 60
4.def fun1(a,b,*,c,d):# Add * Number , representative * The parameters after No. can only be passed by keywords
def fun1(a,b,*,c,d):
print(a)
print(b)
print(c)
print(d)
fun1(10,20,c=30,d=40)
Print :
10
20
30
40
---------------------------
def fun1(a,b,*,c,**d): # Sure
pass
def fun1(a,b,*,c,*d): # Can not be
pass
边栏推荐
- Excel导入,导出功能实现
- 【RTKLIB 2.4.3 b34 】版本更新简介一
- Easy to use shortcut keys in idea
- [algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
- How to reduce the shutdown time of InnoDB database?
- isEmpty 和 isBlank 的用法区别
- Special palindromes of daily practice of Blue Bridge Cup
- [algorithm] sword finger offer2 golang interview question 2: binary addition
- C programming exercise
- Introduction to the daily practice column of the Blue Bridge Cup
猜你喜欢
第一人称视角的角色移动
[algorithm] sword finger offer2 golang interview question 10: subarray with sum K
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
Unity scene jump and exit
There is no red exclamation mark after SVN update
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
FairyGUI增益BUFF數值改變的顯示
Easy to use shortcut keys in idea
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
[Chongqing Guangdong education] Shandong University College Physics reference materials
随机推荐
FairyGUI复选框与进度条的组合使用
Easy to use shortcut keys in idea
记录:下一不小心写了个递归
Combination of fairygui check box and progress bar
Edit distance (multi-source BFS)
FairyGUI条子家族(滚动条,滑动条,进度条)
Meanings and differences of PV, UV, IP, VV, CV
Usage differences between isempty and isblank
(the first set of course design) sub task 1-5 317 (100 points) (dijkstra: heavy edge self loop)
Lock wait timeout exceeded try restarting transaction
Force buckle 1189 Maximum number of "balloons"
FairyGUI循環列錶
Matlab读取GNSS 观测值o文件代码示例
1041 be unique (20 points (s)) (hash: find the first number that occurs once)
闇の連鎖(LCA+树上差分)
GNSS positioning accuracy index calculation
FairyGUI人物状态弹窗
FairyGUI摇杆
MySQL shutdown is slow
MySQL error warning: a long semaphore wait