Python Learning diary day5 function

Source: Internet
Author: User

I. Why to use a function

  1. Avoid face code reuse

2. Improve readability

3. Make the program easier to maintain

Two. Definition and invocation of functions

Def helloword (): #函数定义, parameters can be added in parentheses

Print ("Get out!")

Helloword () #调用函数

Three. function parameters and Local variables  

Parametric the memory unit is allocated only when called, releasing the allocated memory unit immediately at the end of the call. Therefore, the formal parameter is only valid inside the function. Function call ends when you return to the keynote function, you can no longer use the shape parametric

Arguments can be constants, variables, expressions, functions, and so on, regardless of the type of argument, and when a function call is made, they must have a definite value in order to pass these values to the parameter. It is therefore necessary to use the assignment, input and other methods to get the parameters to determine the value

Def calc (x, y): #其中x, Y is the formal parameter

Print (X+y)

Calc (b) #a, B is an argument

1. Default parameters:

def cout (name,sex,country = "CN"):

Print (Name,sex,country)

cout ("yungexing", "F")

2. Key parameters

cout (sex= "F", Name "" Feilie) #关键参数要在默认参数之后

3. Non-fixed parameters

  def user_info (Name,age,*args):
Print (Name,age,args)
User_info ("link", "+", "pig", "GoodGame")
>>>link (' pig ', ' GoodGame ')

Another type of non-fixed parameter
  def user_info (Name,age,**kwargs):
Print (Name,age,kwargs)
User_info ("link", 1000,pet= "pig", total = "GoodGame")
>>>link "pet": ' Pig ', ' total ': ' GoodGame '}
Args and Kwargs are not required to be filled in when the function is called
4. Local Variables
Name = "link"
Def cout_name ():
Print ("Before Change", name)
Name = "Zelda"
Print ("After Change", name)
Cout_name (name)
Print (name)
>>>

Change Zelda
Link
Change the global variables within the function as follows
Name = "link"
Def cout_name ():
Global Name
Print ("Before Change", name)
Name = "Zelda"
Print ("After Change", name)
Cout_name ()
Print (name)
>>>

Before Change link
After change Zelda
Zelda

Three. Nesting functions

Def func1 ():
Def FUNC2 ():
Print ("The first")
Func2 ()
Print ("The second")
Func1 ()
Print ("The Last")
>>>

The first
The second
The last

You cannot call FUNC2 at the outermost layer, or you will get an error. Because the function executes only when it is called

Four. Recursion

Calling itself within a function is a recursive function

  

Def calc (n):
Print (n)
if int (N/2) = = 0:
Return
Else
Return calc (int (N/2))
Calc (10)
>>>

10
5
2
1

Five. higher-order functions

The higher-order function is to pass the function name as a parameter into another function, or the return value has the function name, the example is as follows

def func1 (name,fun2): fun2 (name)

def func2 (eg):
print (eg)
Func1 ("Fate", Func2)
>>>fate

Six. Anonymous functions
  
An anonymous function is a specified function that does not need to be displayed
  
res = map (lambda x:x*x,[1,3,5,7,9])
For I in Res:
Print (i)
>>>

1
9
25
49
81

Python Learning diary day5 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.