11-python-function

Source: Internet
Author: User

1. Definition of function

Definition: A function is a collection of a set of statements by a name (function name) encapsulated, in order to execute this function, just call its function name. The characteristics are as follows:

    • Reduce duplicate code
    • To make the program extensible
    • Make programs easier to maintain

2. Syntax definition

2.1 Basic Definitions

1 deffunc1 ():2     " "test1" "  #function of declaring functions3     Print("Test function1")4     return0#return value5 6Func1 ()#calling Functions7x = Func1 ()#assign value to x for function return value8 Print(x)

2.2 Functions with parameters

The parameters of the function are also divided into formal parameters and actual parameters :

    • Parametric: The memory unit is allocated only when it is called, and the allocated memory unit is freed immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. The parameter variable can no longer be used after the function call finishes returning the keynote function.
    • Actual parameters: Can be constants, variables, expressions, functions, and so on, regardless of the actual argument is the type of amount, when making a function call, they must have a definite value, in order to pass these values to the parameter. Therefore, the parameter should be determined by the method of assignment, input and so on beforehand.
1 deffunc1 (x, y):2     Print(x)3     Print(y)4 5Func1 (ON)#positional parameters, actual participation in parameter one by one corresponds to position6Func1 (Y=2,x=1)#keyword parameter, regardless of location. However, it is important to note that the keyword parameter must be after the position parameter. 7 8Func1 (10,y=20) 9 #func1 (x=10,20) This is not possible, because the keyword parameter cannot be written in front of the position parameter

default parameter : The default argument must be placed after the formal parameter.

1 #The existing code is as follows:2 defStu_register (name,age,country,course):3     Print("----Registered Student information------")4     Print("Name:", name)5     Print("Age :", age)6     Print("Nationality:", Country)7     Print("Course:", course)8  9Stu_register ("Wang Shanbao", 22,"CN","Python_devops")TenStu_register ("Zhang Jiaochun"21st"CN","Linux") OneStu_register ("Liu Lao Gen", 25,"CN","Linux") A  - #The country are found to be the same, so you can give the parameter a default value: - defStu_register (name,age,course,country='CN'):#默认参数必须放形参的后面 the     Print("----Registered Student information------") -     Print("Name:", name) -     Print("Age :", age) -     Print("Nationality:", Country) +     Print("Course:", course) -   +Stu_register ("Wang Shanbao", 22,"Python_devops") AStu_register ("Zhang Jiaochun"21st"Linux") atStu_register ("Liu Lao Gen", 25,"Linux")

non-fixed parameter : If your function is not determined by how many parameters the user wants to pass in, you can use the non-fixed parameter

1 #*args: receive n positional parameters, convert narimoto group 2 defStu_register (Name,age,*args):#*args will change multiple incoming parameters into a tuple form, and *args must be placed after formal parameters and default arguments 3     Print(Name,age,args)4   5Stu_register ("Alex", 22)#Alex () #输出: Alex () #后面这个 () is args, just because the value is not passed, so it is empty tuple6 7Stu_register ("Jack", 32,"CN","Python")#output: Jack (' CN ', ' Python ')8 9 #**kwargs: receives n keyword arguments, translates into dictionaries Ten defStu_register (Name,age,*args,**kwargs):#**kwargs will turn multiple incoming parameters into a dictionary form, and **kwargs must be placed after *args  One     Print(Name,age,args,kwargs) A    -Stu_register ("Alex", 22)#output: Alex () {} #后面这个 {} is Kwargs, just because there is no value, so an empty dictionary -   theStu_register ("Jack", 32,"CN","Python", sex="Male", province="Shandong")#output: Jack (' CN ', ' Python ') {' Province ': ' Shandong ', ' sex ': ' Male '}

11-python-function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.