Function basis
Defined functions
Function is an idea.
In the program, a function that has the function of a function, the tool is ready to talk in advance is the definition of the function, encountered a scene that is brought on by the calling function.
Why use function
If we had not using the function, you will encounter three problems when writing programs:
1. The process is lengthy
2. The extension of the program's poor
3. poor readability of the program
How to use the function
Defined Functions
Define function, after the call:
- Defined Functions
def 函数名(param1,param2....):
"""
函数功能的描述信息
:param1 描述
:param2 描述
:return 返回值
code1
code2
code3
"""
return 返回值
call function
函数名(param1、param2...)
Registration function
def register():
# 注册功能
username = input('username').strip()
pwd = input('password').strip()
with open('38a.txt','a',encoding='utf8') as fa:
fa.write(f"{username}:{pwd}\n")
fa.flush()
register()
# 复用
register()
register()
Log function
def login():
inp_username = input('username:').strip()
inp_pwd = input('password:').strip()
with open('38.txt','rt',encoding='utf8') as fr:
for user_info in fr:
user_info = user_info.strip('\n')
user_info_list = user_info.split(':')
if inp_username == user_info_list[0] and inp_pwd == user_info_list[1]:
print('login succesdful')
break
else:
print('failed')
login()
Function definition stage
def func():
bar() # 不属于语法错误,不会报错
print('*'*10)
Only detect syntax, the code does not execute the function body
Function call stage
def bar():
print('from bar')
def foo():
print('from foo')
bar()
foo()
from foo
from bar
def foo():
print('from foo')
bar()
def bar():
print('from bar')
foo()
from foo
from bar
Three forms of defined functions
No-argument function
When you define a function parameter is a function of the media to accept the external value of the body, in fact, a variable name
No phase function parameter in the brackets, the function is called with no arguments. Note that: when you define lunch, means that there is no need to call the incoming parameters.
If the function does not need to rely on an external body of the incoming code logic value, no parameters have to define the function.
def func():
print('hello world')
func() # hello world
There are function parameters
There are parameters in the function definition stage brackets, known to have a function parameter. Note that: there are parameters defining, thought must also be passed in the parameters when calling.
If the function code logic relies on the external body of the incoming values, parameters have to have a defined function.
def sum_self (x,y):
res = x + y
print(res) # 3
sum_self(1,2)
Empty function
When you know you need to achieve a certain function, but do not know how to use code, you can write a temporarily empty function, and then to implement other functions.
def func():
pass
Function's return value
What is the return value
After some internal code function result obtained by the processing logic.
def func():
name = 'shiqi'
return name
name = func()
print(name)
shiqi
Why should there be a return value
return function is a sign of the end, there can be multiple return within a function, to return as long as the execution, the function will be executed.
return return value can return any data type
return Return Value No limit to the number that can be used to return multiple values separated by commas.
0: return None
1: The return value is the value itself
A plurality of: The return value is a tuple
def max_self(salary_x,salary_y): if salary_x > salary_y: return salary_x else: return salary_y max_salary = max_self(20000,30000) print(max_salary*12)
360000
返回多个值: def func(): name = 'shiqi' age = 17 hobby_list = ['sing','junm','rap','basketball'] return name,age,hobby_list name,age,hobby_list = func() print(f"i am {name},my age is{age},i like {hobby_list}")
i am shiqi,my age is17,i like ['sing', 'junm', 'rap', 'basketball']