Python entry Function-1

Source: Internet
Author: User

This catalogue:

I. Introduction to Functions

Second, the definition of function

Iii. three types of defined functions

Iv. phase of a function call

V. Return value

==========================================================================

I. Introduction to Functions

1. What is a function? Why do you have a function?

1.1. Tools with a specific function (analogy) >>> functions

1.2 Procedures for preparing tools (analogy) >>> definition of functions

1.3 Tools are used directly, using their specific functions >>> functions of the call

1.4 There are no functions, the program will have the following disadvantages:

A. Unclear organizational structure and poor readability

B. Difficult to manage and maintain, poor expansibility

C. Code redundancy, repeating

# 1 Defining functions def Print_sym (sym*count):    print(sym*count)def  print_msg (msg ):    print('\033[35m%s\033[0m' %msg)

Second, the definition of function

1. Syntax structure of functions

1.1 The structure of the function, Def is the meaning of define, represents the definition

    def   Function name (parameter 1, parameter 2, Parameter 3 ...) ):  " This is used to add document descriptions, to illustrate functions, to increase readability  "
Code block section

1.2 functions can be called directly by the function name, which must be able to prepare the function of the reaction function

1.3 Code block section is code for function function implementation

Note the point:

# both the function and the variable are memory-intensive, and the procedure is similar, but the function is not a variable

    

Iii. three types of defined functions

1. With the parameter function

The 1.1 parameter is the value of the part of the external calling function passed into the function

The code block logic in the 1.2 function requires the function caller to pass in the value

 def   Max2 (x, y):    This is a relatively sized function that returns a larger number      if  x >y:   Print   (x)  else  :  print   (y)  #   call function Max2  max2 (+) 
 def   auth (username,pwd):    This function is used to validate user-entered user names and passwords   < span>  landed successfully!   " )  else  :  print  ("   Login failed   " "  #   call Auth ()  auth () 
defInteractive ():" "This function is used to return the user name and password entered by the user" "Inp_name= Input ('Username>>>:'). Strip () Inp_pwd= Input ('Password>>>:'). Strip ()returnInp_name, Inp_pwddefAuth (Inp_name, pwd):" "This function is used to verify that the result of return is a tuple type" "Res=Interactive () x=Res[0] y= Res[1]    Print(x)Print(y)

2. No parameter function

1.1 The function itself defines no arguments, and the caller does not need to pass in parameters

def Print (): " " This function is used to show the function of no argument ""    Print ('\033[35m--------------------\033[0m')    print('  --------------do not need to pass in Parameters------------')

1.2 The code logic of the function body does not require the caller of the function to pass in the value

defauth ():" "This function is used to show the function of no argument" "Inp_name= Input ('Username>>>:'). Strip () Inp_pwd= Input ('Password>>>:'). Strip ()ifUsername = = Inp_name andPWD = =inp_pwd:Print('Landing Success! ')    Else:Print('Login Failed')#Call Auth ()Auth ()

3. Null function

Nothing is done. A function is an empty function.

Even if it is an empty function, it will have the following properties:

[' __call__ ', ' __class__ ', ' __closure__ ', ' __code__ ', ' __defaults__ ', ' __delattr__ ', ' __dict__ ', ' __doc__ ', ' __format__ ' ', ' __get__ ', ' __getattribute__ ', ' __globals__ ', ' __hash__ ', ' __init__ ', ' __module__ ', ' __name__ ', ' __new__ ', ' __reduc e__ ', ' __reduce_ex__ ', ' __repr__ ', ' __setattr__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' func_closure ', ' Func_c ' Ode ', ' func_defaults ', ' func_dict ', ' func_doc ', ' func_globals ', ' func_name '

  

# Tips: When we are in the process of writing a program, we encounter a sticky place, we can add a pass at the end of the function, temporarily skip the thought of the open space. 

Iv. phase of a function call

1. Calling the code: you must first define and then call

The definition phase does not execute code, only the syntax is detected

    

2. Call Mode: function name + parentheses

The call stage locates the memory address of the function according to the function name, executes the code body of the function

    

V. Return value

1. When should there be a return value

After the function code body finishes executing, you need to return a result to the caller

2. Return value of three cases

2.1 When no return is returned in the function code block, return to none

    

2.2 If the return value is followed by a value, the return value itself

    

The value returned by the 2.3 return is separated by commas, returns multiple values, and forms a tuple (type)

2.4 If there is more than one return in the body of the function, return the value of the first return (return represents the end of the function)

    

    

# return value has no type limit # multiple return can be written within a function, but only the first return will end # return is the mark of the end of the function

Python entry Function-1

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.