当前位置:网站首页>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 .
边栏推荐
- 2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
- Unity application class and data file path
- 【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)
- Leetcode 180 Consecutive numbers (2022.06.29)
- 《谁动了我的奶酪》读后感
- 网络变压器怎么判断好坏?网络滤波变压器坏了一般是什么症状?
- Revit Secondary Development - - Project use Panel features not opened
- Unity3d position the model, rotate, drag and zoom around the model to obtain the center point of the model
- The minecraft server address cannot be refreshed.
- Rotation, translation and scaling of unity VR objects
猜你喜欢

Unityshader learning notes - Basic Attributes

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

图扑软件基于钻孔数据的三维地质模型可视化

Super comprehensive summary | related improvement codes of orb-slam2 / orb-slam3!

Writing unityshader with sublimetext

Intellj idea jars projects containing external lib to other project reference methods - jars

Unity project hosting platform plasticscm (learn to use 1)

VFPBS上传EXCEL并保存MSSQL到数据库中

Assembly learning tutorial: accessing memory (3)

Bev instance prediction based on monocular camera (iccv 2021)
随机推荐
Chapter 9 of OpenGL super classic (version 7): fragment processing and frame buffering
[typescript] cannot redeclare block range variables
Revit Secondary Development - - Project use Panel features not opened
Log writing specification
旋转框目标检测mmrotate v0.3.1 学习配置
如何写论文
Pytorchcnn image recognition and classification model training framework
unity 扫描圈 圆扩展方法
Initial environment configuration of the list of OpenGL super classic (version 7) vs2019
Ugui uses its own function to realize reverse mask
Unity3d learning notes-1 (C # learning)
Responsive flow layout
How to judge the quality of network transformer? What symptom is network filter transformer broken?
Chapter 8 primitive processing of OpenGL super classic (version 7)
Unityshader learning notes - Basic Attributes
Unity shortcut key
[typescript] experimentaldecorators of vscode stepping pit
图扑软件基于钻孔数据的三维地质模型可视化
Access is denied encountered when vfpbs calls excel under IIS
Unity multiple UI page turning left and right