当前位置:网站首页>Learning about functions QAQ
Learning about functions QAQ
2022-06-30 05:22:00 【Programming is too hard】
Function definition and call
Definition : utilize def Function name (): The format of
For function calls , Write the function name directly () that will do .

Functions with parameters
Write directly in the parentheses of the function name .
Function with return value
return
It can print directly , You can also use a variable to receive , Printout in progress .
Multiple functions that return values
stay return You can then write multiple return values , When receiving the return value , Just write the variable names in order .
Small exercise
Print single line
Write a function that prints a horizontal line
def printinfo():
print("----------")
printinfo()
Print custom number of lines
# Write a function , You can use the input parameters , Print out the horizontal line of the custom number of lines
def printinfo():
print("----------")
def printinfos(num):
# i = 1
# while i <= num:
# printinfo()
# i += 1
for i in range(0, num):
printinfo()
num = int(input(" Please enter the number of pieces to print :"))
printinfos(num)
Sum three numbers
Write a function to find the sum of three numbers
def add(a,b,c):
sum = a + b + c
print(sum)
add(1,2,3)
averaging
Write a function to find the average of three numbers
def add(a,b,c):
sum = a + b + c
return sum
def avg():
a = add(1,2,3)
num = a/3.0
print(num)
avg()
Local and global variables
local variable
The scope of a local variable is in the body of the function that defines it , Leaving the function body scope ends .
Traversal with the same name can be defined in functions that are not used , They don't influence each other .
Global variables
The global variable scope is the entire file .
Can be used by all functions .
Global variables and local variables use the same variable name
When global variables and local variables are defined with the same variable name , In a function that defines a local variable , Local variables are preferred , Functions that do not define variables use global variables .
Function to change the value of a global variable
Use global Add variable names to declare global variables , Then you can modify the global variables .
边栏推荐
- Unity animator does not clip animation to play animation in segments
- Chapter 8 primitive processing of OpenGL super classic (version 7)
- [typescript] experimentaldecorators of vscode stepping pit
- GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
- El table lazy load refresh
- 【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
- Nestjs中控制器和路由的配置使用
- Unity + hololens common basic functions
- Unity notes_ SQL Function
- Tensorflow2 of ubantu18.04 X installation
猜你喜欢
![[Motrix] download Baidu cloud files using Motrix](/img/d3/f3d29468367cf5011781f20f27a5c8.jpg)
[Motrix] download Baidu cloud files using Motrix

Unity project hosting platform plasticscm (learn to use 2)

Rotation, translation and scaling of unity VR objects

企事业单位源代码防泄露工作该如何进行

旋转框目标检测mmrotate v0.3.1 训练DOTA数据集(二)

虚析构和纯虚析构

Ugui uses its own function to realize reverse mask
![[learning notes] AssetBundle, xlua, hot update (use steps)](/img/59/9d9f31cfe55a908f2f0705e95ecc05.jpg)
[learning notes] AssetBundle, xlua, hot update (use steps)

旋转框目标检测mmrotate v0.3.1入门

RedisTemplate 常用方法汇总
随机推荐
Golan no tests were run: fmt Printf() &lt; BUG&gt;
Chapter 9 of OpenGL super classic (version 7): fragment processing and frame buffering
Unity camera control
Unity project hosting platform plasticscm (learn to use 2)
Unity- the camera follows the player
Detailed explanation of the loss module of mmdet
Xctf--Web--Challenge--area Wp
East Tower attack and defense world - XSS bypasses the safety dog
ParticleSystem in the official Manual of unity_ Collision module
旋转框目标检测mmrotate v0.3.1入门
C # uses monopinvokecallback to directly call back C # function
Untiy3d controls scene screenshots through external JSON files
Question mark (?) in Cron expression Use of
Gradient clip in dqn
Pytorchcnn image recognition and classification model training framework
Unity Catmull ROM curve
Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
Word frequency statistics (string, list)
Chapter 8 primitive processing of OpenGL super classic (version 7)
Unity3d get screen width and height