Functions of Python

Source: Internet
Author: User
Tags define local ming variable scope

One, what is a function?

Definition: A function that encapsulates a set of statements by a name (function name), and to execute the function, simply call the name of its functions

Characteristics:

1. Reduce the duplication of code

2. Scalability of the Code

3. Easy maintenance of the code

Syntax definitions

Def SayHello ():

Print (' Hello,i ' m Good Boy)

SayHello ()

You can also pass in parameters directly

def add (x, y):

res = X+y

return res

result = Add (3,5)

Print (Result)

Second, the function parameter

parameter variables are allocated only when the memory unit is called, freeing the memory at the end of the call, that is, the shape parametric does not consume any memory when it is not called. Therefore, the formal parameter can only be valid inside the function.

arguments can be constants, variables, expressions, functions, and so on, but when a function is called, it must have a definite value to pass to the parameter.

Four ways to pass a function

1. Location Transfer

def info (name,age,sex):     return ' There's a new name called%s, this year%s, gender%s '%(name,age,sex) print (info ('mingo',  male '))
View Code

In accordance with position one by one, multiple or fewer arguments will be an error

2. Keyword Delivery

def info (name,age,sex):     return ' There's a new name called%s, this year%s, gender%s '%(name,age,sex)print(info (name='mingo', sex='  male ', age=23))
View Code

Keyword delivery is not sequential, as long as your name is right, but one thing to note is that the keyword argument cannot appear in front of the position parameter

For example:

def info (name,age,sex):     return ' There's a new name called%s, this year%s, gender%s '%(name,age,sex)print(info (name='mingo', sex='  male ', 23))
View Code

3. Default delivery

defInfo (name,age,sex='female'):    return 'There's a new name called%s, this year%s, gender%s'%(Name,age,sex)Print(Info (name='Mingo', age=23))#sex doesn't write default girlPrint(Info (name='Mingo', sex='male', age=23))#The sex write overrides the default value
View Code

4 Parcel delivery (used when unsure how many parameters the user wants to pass in)

defInfo (*args):Print(args)Print(Type (args)) info (1,2,4,'a','Mingo'21st'nan') Printing results (1, 2, 4,'a','Mingo'21st'nan')<class 'tuple'>
View Code

Args collects the arguments passed in and becomes the form of a tuple, which is the name of a specification and can be changed to another name, such as *aa

defInfo (* *Kwargs):Print(Kwargs)Print(Type (Kwargs)) info (name='Mingo', age=21,sex='nan') Print result {'name':'Mingo',' Age': 21,'Sex':'nan'}<class 'Dict'>
View Code

Kwargs will accept the parameters of the Key-value form and become the form of a dictionary.

defInfo (*args,**Kwargs):Print(args)Print(Kwargs) info (1,2,name='Mingo', age=21,sex='nan') Printing results (1, 2){'name':'Mingo',' Age': 21,'Sex':'nan'}
View Code

The above various forms can cooperate with the use, if has the opportunity to see the small partner or oneself to try more, I did not post the code

Three, local variables

Name ="Mingo"defchange_name (name):Print("before change:", name) name="Brother Ming, a man who can't afford a house."    Print(" after change", name) change_name (name)Print("look outside, do you have a name change?", name) print result before change:mingoafter change Ming, a man can't afford to buy a room look at name changed? Mingo
View Code

Global vs. local variables

A variable defined in a subroutine is called a local variable, and a variable defined at the beginning of the program is called a global variable. The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable. When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work in other places. To modify a global variable to call global only internally, it is not possible to make a change to an overall variable.four, return valueBelieve that many people have a question about return?

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

Attention:

    1. The function stops executing and returns the result as soon as it encounters a return statement, so can also be understood as a return statement that represents the end of the function
    2. If return is not specified in the function, the return value of this function is None

So, what's the use of eggs? Look at your needs.

Functions of Python

Related Article

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.