当前位置:网站首页>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 C trigonometric function, right triangle corner calculation
- Summary of common loss functions in pytorch
- Pytorch的安装以及入门使用
- Initial environment configuration of the list of OpenGL super classic (version 7) vs2019
- Virtual and pure virtual destructions
- How to use js to control the scroll bar of moving div
- Assembly learning tutorial: accessing memory (3)
- Ugui uses its own function to realize reverse mask
- Nestjs introduction and environment construction
- Access is denied encountered when vfpbs calls excel under IIS
猜你喜欢

Why can transformer break into the CV world and kill CNN?
![[note] usage model tree of the unity resource tree structure virtualizingtreeview](/img/3e/fe5610c797a14554ad735172c3ab54.jpg)
[note] usage model tree of the unity resource tree structure virtualizingtreeview

Unity2019.3.8f1 development environment configuration of hololens2
![[notes] unity Scrollview button page turning](/img/c7/47c4056871d0212ac61524539f0d0e.jpg)
[notes] unity Scrollview button page turning

Unity shader flat shadow
![[vcs+verdi joint simulation] ~ take the counter as an example](/img/fb/214a4e65c53503ecbc38a5e43523cf.png)
[vcs+verdi joint simulation] ~ take the counter as an example

Rotation, translation and scaling of unity VR objects

How to prevent source code leakage in enterprises and institutions

【LeetCode】Easy | 232. Using stack to realize queue (pure C manual tearing stack)

How to judge the quality of network transformer? What symptom is network filter transformer broken?
随机推荐
Responding with flow layout
Unity obtains serial port data
炒美原油的国际交易平台如何能保障资金安全呢?
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
Nestjs configures static resources, template engine, and post examples
Pytorch的安装以及入门使用
2021-07-29 compilation of Cura in ubantu18.04
Log writing specification
2021-06-17 solve the problem of QML borderless window stretching, window jitter and flicker when stretching and shrinking
Does the tester need to analyze the cause of the bug?
Unity call Exe program
MinGW-w64下载文件失败the file has been downloaded incorrectly!
Unity limited time use limited trial time and use times
Nestjs introduction and environment construction
Nestjs入门和环境搭建
PyGame. Why can't I exit when I click X in the window? I can only exit when I return idle
Pytorchcnn image recognition and classification model training framework
Nestjs中控制器和路由的配置使用
《谁动了我的奶酪》读后感
Win10 vs2015 compiling curaengine