This article brings you relevant knowledge about python, which mainly introduces related issues about functions, including function calls, defining functions, function parameters, function return values, and variable functions. Domains and other contents, let’s take a look at them below. I hope it will be helpful to everyone.
Recommended learning: python video tutorial
A function is an organized, reusable code segment used to implement a single or related function. Functions can improve application modularity and code reuse. You already know that Python provides many built-in functions, such as print(). But you can also create your own functions, which are called user-defined functions.
In short, functions are used a lot in our daily life, but most of them are officially defined functions. We can call them directly, such as input(), print(), etc., but it We don't care how it is defined. If we need to reuse a complex code block in large quantities in our code, then we can define a function to represent this code block and call it directly when needed! !
A function consists of three parts: function name, parameters and return value.
The function name is the identifier of the function.
The parameters of the function are to provide data to the function when calling the function.
name = input("请输入你的姓名:")list = len(name)print(list)
Here, input, len, and print are the function names, the ones in the function brackets are the parameters, and the ones on the left side of the equal sign are the return values.
Calling a function: Generally add parentheses to the function name. Parameters can be filled in in parentheses to provide data for the function. Of course, some functions do not require parameters (list.clear()), and some functions must pass parameters (list.append()).
You need to use the def (define) keyword to define a function and it must end with a colon.
The function must be defined first before calling
def name(): print('苏凉')def QQ_num(): print('787991021')def Total(): name() QQ_num() Total()
Define the function:
Function header: keyword def custom function name Add parentheses and end with a colon. def name(),def QQ_num(),def Total()
Function body: The function that needs to be implemented by the function. That is, the function body must be indented by 4 characters. A tab key.
Note: The execution of functions is from top to bottom, that is, the function must be defined first before calling.
The parameters of the function can make the function we define more flexible.
Note: If parameters are passed in when defining the function, the parameters must also be specified when calling.
#When passing parameters, you can pass in one parameter or multiple parameters.
# 传入一个参数def list(len): print('+' * len)list(5)# 传多个参数def list2(num1 , num2): print(num2 * num1)list2('*',15)list2(5,10)
When calling a function, the actual value (actual parameter) is given, so that the defined parameter (formal parameter) will be assigned a value.
Note: When passing in multiple parameters, you need to pay attention to whether the number and order of parameters are correct. Different functions have different meanings. .
The function can return a single value or multiple values. Use return to return the value.
Note: When the function executes return, the function execution ends. That is, the function body after return will not be executed again.
def num(age,sex): if age200: return else: return age,sex x = int(input('输入年龄:'))Sex = input('输入性别:')num ,sex = num(x,Sex)print(num,sex)
Use as many values as the function returns to receive, otherwise an error will be reported. In this case a single value is returned respectively.
A special case is to use a variable to accept, and the returned value is a tuple type!
result = num(x,Sex)print(result)
Summary: The function can return a single value or multiple values. When multiple values are returned, multiple corresponding variables need to be used to receive the values returned by the function. Value, if only one value is received, a tuple type value is returned.
变量的作用域:即是指在那个地方可以使用变量。这就涉及到了全局和局部两种变量。
全局(global)变量:在函数外定义的变量。无论在函数体内或者函数体外都可以使用! ?全局变量在函数体内只能使用而不能直接修改!!
局部(local)变量:在函数内定义的变量,在函数内定义的变量,只能在函数体内使用和修改,在函数外调用就无效了。 在函数内可以定义一个名字和函数外一样的变量,但他们的意义时不一样的!!
a = 15 #这里a为全局变量def num(): a = 5 #这里a为局部变量,名字可以相同但代表不同的值 print(a)num()print(a)
结果:
这里可以看到局部变量是不能修改全局变量的值的。
a = 15 #这里a为全局变量def num(): # 在函数体内可以使用全局变量 print(a) num() #结果15print(a) #结果15
在函数体内是可以使用全局变量的
a = 15 #这里a为全局变量def num(): global a #定义全局变量 a = 5 print(a)num() #结果5print(a) #结果5
若想要在函数体内修改全局变量,则需在修改之前,定义全局变量,此时函数体内的变量a为全局变量,不再是函数体内定义的局部变量了。
推荐学习:python视频教程
The above is the detailed content of Detailed explanation of python basic syntax functions. For more information, please follow other related articles on the PHP Chinese website!