当前位置:网站首页>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 202. 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 303. 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 604.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
边栏推荐
- 【无标题】
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- FairyGUI按钮动效的混用
- 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
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
- Sharing ideas of on-chip transplantation based on rtklib source code
- GNSS定位精度指标计算
- [untitled]
- Knowledge system of digital IT practitioners | software development methods -- agile
- 【干货】提升RTK模糊度固定率的建议之周跳探测
猜你喜欢

Excel导入,导出功能实现

There is no red exclamation mark after SVN update

341. Flatten nested list iterator
![[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和](/img/d5/4bda133498f71ae9fd7a64c6cba8f0.png)
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和

使用rtknavi进行RT-PPP测试

MySQL 三万字精华总结 + 面试100 问,吊打面试官绰绰有余(收藏系列

闇の連鎖(LCA+树上差分)
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
![[algorithm] sword finger offer2 golang interview question 2: binary addition](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[algorithm] sword finger offer2 golang interview question 2: binary addition

Code example of MATLAB reading GNSS observation value o file
随机推荐
Fabrication of fairygui simple Backpack
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
KF UD decomposition pseudo code implementation advanced [2]
【GNSS】抗差估计(稳健估计)原理及程序实现
FairyGUI摇杆
rtklib单点定位spp使用抗差估计遇到的问题及解决
Office prompts that your license is not genuine pop-up box solution
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
FairyGUI按钮动效的混用
染色法判定二分图
堆排序【手写小根堆】
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
FairyGUI增益BUFF数值改变的显示
记录:Navicat Premium初次无法连接数据库MySQL之解决
FairyGUI簡單背包的制作
【无标题】
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Role movement in the first person perspective
服务未正常关闭导致端口被占用