当前位置:网站首页>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 .
边栏推荐
- Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself
- After reading who moved my cheese
- Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
- Postman 做测试的 6 个常见问题
- Unity Catmull ROM curve
- pytorch中常用损失函数总结
- Pit of smoothstep node in shadergraph
- 9. naive Bayes
- Summary of common loss functions in pytorch
- Unity application class and data file path
猜你喜欢
PWN Introduction (2) stack overflow Foundation
[vcs+verdi joint simulation] ~ take the counter as an example
Terminal convenient SSH connection
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
[learning notes] AssetBundle, xlua, hot update (use steps)
Records of problems encountered in unity + hololens development
Pytorch的安装以及入门使用
Rotation, translation and scaling of unity VR objects
Chinese pycharm changed to English pycharm
Intellj idea generates jar packages for projects containing external lib to other projects. The method refers to the jar package written by itself
随机推荐
Unity3d learning notes-1 (C # learning)
[typescript] cannot redeclare block range variables
PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
Unity3d- use animator and code to control task walking
Fifty years ago, the go code first submitted by the inventor of Hello world was as long as this
Bev instance prediction based on monocular camera (iccv 2021)
OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
Unit asynchronous jump progress
Expansion method of unity scanning circle
中文版PyCharm改为英文版PyCharm
14x1.5cm vertical label is a little difficult, VFP calls bartender to print
How to prevent source code leakage in enterprises and institutions
PWN入门(2)栈溢出基础
Unity publishing /build settings
【VCS+Verdi联合仿真】~ 以计数器为例
Nestjs introduction and environment construction
Unity project hosting platform plasticscm (learn to use 1)
Unity screenshot method
Unity project hosting platform plasticscm (learn to use 2)
mmdet之Loss模块详解