Python function 01

Source: Internet
Author: User

What is a function

Definition: A function is a collection of a set of statements by a name (function name) encapsulated, in order to execute this function, only need its function name call can

Characteristics:

  1. Reduce duplication of code

2. Make the program extensible

3. Make the program easier to maintain

Syntax definitions

def Weekday ():     Print ("hello") weekday ()

can take parameters

def Calt (x, y    ): = x * y    return##返回函数执行结果 (any value can be, because it only needs to return a result, and then take it to do something else)CALTT = Calt (10,20)print(CALTT)

return value

To get the result of the function execution, you can return the result using the return statement

Attention:

1. The function will stop executing and return the result as soon as it encounters a return statement, so it can also be understood that the return statement represents the end of the function.

2. If return is not specified in the function, the return value of this function is None

##嵌套函数Name ="Caicai"defname_list (): Name="Caicai"        defname_list2 (): Name="Caicai"            Print("This is the SAN layer print:", name) name_list2 ()Print("This is the second layer of printing:", name) name_list ()Print("the outermost:"Name
# # #递归 def Calt (n):        Print (n)        if  Int (N/2) >0:           return calt (int (N/2)) Calt (+) " characteristics of recursion:    1. There must be a clear end condition    2. Each time a deeper level of recursion is reached, the problem size should be reduced by 3 compared to the last recursion    . The recursive efficiency is not high, the recursive hierarchy too much will cause the stack overflow (in the computer, the function call is implemented through the data structure of the stack), Whenever a function call is entered, the stack adds a layer of frames, and whenever the function returns, the stack is reduced by a layer of frames. Because the size of the stack is not wireless, too many recursive calls can cause stack overflow)'

Python function 01

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.