Python---------functions

Source: Internet
Author: User

Why should I use a function?

Because if there are no functions, our usual code will have a lot of repetition.

Avoid code reuse

Improve the readability of your code

second, the definition and invocation of functions:

  

1. def function name (parameter 1, parameter 2)

"' Function comment '

Print (' function body ')

return value

function is to encapsulate a function

Defined:

  The DEF keyword begins with a space followed by a function name and parentheses, and a colon at the end of the

DEF is fixed and cannot be changed

The definition of a function name is the same as a variable: alphanumeric underscore any combination, not the beginning of a number cannot be a keyword to be descriptive

Call to function

return value = function name (parameter 1, parameter 2) #记得函数名后面加括号

Let's start by defining a function that calculates the length of a string. If our Len () function is not available now, then we need to define a function to find the length of the string. It would be cumbersome to write the same code once and for all. That's when our function is on the pitch.

defLength (): s='Hello World'length=0 forIinchS:length+=1Print(length)Print(Length ())#because there is no return value you print a null at the enddefLength (): s='BJBBABJSAOFSA'Num=0 forIinchS:num+=1returnnum m=Length ()Print(m)#Print (length ()) #return的时候你可以定义一个变量来接收 can also not receive direct printing but if print is not, it is generally better not to write print too Lowle in the function
View CodeThe return value of the function:

Defining functions generally do not write print in a function

Return

1, the function is encountered return on the end of the function

2. Return the value to the caller of the function

No return

Return None

Return a value what the value is and return what

Let's see, there's only one return in the right and the function because the first one touches the return and ends the function.

def func (A, B, c):     return a     return b Print (Func (3, 7, 8))3     # returns only 3 of the first return  because the first return is the end of the function. The other return exists without meaning.

  

Pass parameters of the function:

Two aspects:

Actual parameter angle:

  1 formal parameters and actual parameters

  

def # this A is the form parameter used to receive the actual parameter.    return L Print (My_len (3))  # actual parameters actually exist.
View Code

2 position parameter one by one corresponding

  

def func (A, B, c):     return A, B, C Print (Func (2, 3,700))   # one by one corresponds and is printed as a tuple

3. Mixed parameters-----> keywords always in the last face

  

def # The formal parameters and the actual parameters are directly assigned to the value to be placed in the end will be error, this you see B directly assigned but placed in the middle of the error    return A, B, C, Print (Func (2, 3,b=700))

  

def func (A, C, b=40):    return A, B, C  # This is right, it's  all in the back.  Print(func (2, 3,b=700))

If you return several values together return then the last return value is displayed in a tuple

Default parameters:

defInfo (username,sex='male'):#define a default parameter sexWith open (' One', encoding='UTF-8', mode ='a') as F:#A is the addition of contentF.write ('{}\t{}\n'. Format (username,sex)) whileTrue:username= Input ('Enter your name: (The boys start with 1)'). Strip ()if '1' inchUsername:username=username[1:] Info (username)Else: Info (username,sex='female')
View Code

Python---------functions

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.