Python3 function One

Source: Internet
Author: User

What is a function?

The term function is derived from mathematics, but the concept of "function" in programming is very different from the function in mathematics, and the function in programming has many different names in English. In basic it is called subroutine (sub-process or subroutine), in Pascal is called procedure (process) and function, in C only function, in Java is called method.

One: Problems with functions and non-use of functions 1, 1 do not use functions
    1. Unclear organizational structure
    2. Code redundancy
    3. No unified management and difficult to maintain
1, 2 questions about using functions
    1. Once created can be called multiple times, enhance the reusability and readability of the code, the function in the code block of variables will be assigned value, but at the end of the call, the value of the variable will be emptied, will not occupy memory space.
    2. functions can improve the modularity of the application, and the reuse of the code. You already know that Python provides a number of built-in functions, such as print (). But you can also create your own functions, which are called user-defined functions.
Two: function classification:
    1. Built-in functions

Python3 commonly used built-in functions, Python English official documents detailed description: Click to view, in order to facilitate viewing, the summary of the built-in functions recorded.

    1. Custom functions

You can define a function that you want to function.

Three: Why to define a function

Often, some commonly used function modules are written into functions, which are placed in function libraries for easy selection. Be good at using functions to reduce the amount of repetitive programming segments.

Use of functions:

1 Define First

2 Call again

A function is a variable, and a variable must be defined before it is used, not defined and directly referenced by a function, which is equivalent to referencing a non-existent variable name.

Code demo?

def foo ():

Print (' from foo ')

Bar ()

Print (foo)

will not error, need to clear the use of functions into the definition phase and call stage

Four: What does the definition function do?

Only the syntax is detected and no code is executed.

Five: How to define functions (function names can reflect their meaning)
    1. Definition syntax for functions
    2. The function code block begins with a def keyword followed by the function identifier name and parentheses ().
    3. Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
    4. The first line of the function statement can optionally use the document string-for storing the function description.
    5. The function contents begin with a colon and are indented.
    6. Return[expression] End Function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
Examples of definitions of 5, 1 functions:

def function name (ARG1,ARG2,ARG3):

Comments

function body

return value

The function name is usually a verb
Parameters
Return: There can be multiple return inside the function, but only once, the function ends the call,
and returns the value after the return as the result of the function execution.

def foo1 (): #函数名
Print ("Hello,i ' m nobody!") #函数体

Foo1 () #调用函数

Six: Define three forms of a function
    1. No parameters: The scenario simply performs some action, such as interacting with the user, printing
    2. Parameter: It is necessary to perform the corresponding logic, such as statistical length, to calculate the maximum minimum value, according to the parameters passed in externally.
    3. Empty function: Design code structure
Seven: Calls to functions

1 Find First Name

2 calling code by name

The return value of the function?

0->none

1-> returns 1 values

More than one tuple

When should I have it?

    1. Call the function, after a series of operations, finally to get a definite result, you must have a return value
    2. Usually have the parameter function need to have the return value, the input parameter, after computation, obtains one final result

When do I need to have it?

    1. Call the function, just perform a series of operations, and finally do not need to get any results, there is no need to have a return value
    2. Normally no parameter function does not need to have a return value
Eight: Three forms of function calls

1 Statement form: foo ()

2 expression form: 3*len (' Hello ')

3 parameters for another function: Range (len (' Hello '))

Nine: Parameters of the function:

1 formal parameters and argument definitions

2 parameter is variable name, argument is variable value, function call binds value to First name, function call ends, unbind

3 Specific applications

Positional parameters: Parameters defined in left-to-right order

Positional parameters: Required parameter

Positional arguments: Pass values to parameters by location

Keyword parameter: Define an argument in the form of Key=value

No need to pass values by location as parameter

Note the issue:

1. Keyword arguments must be to the right of the location argument

2. Cannot repeat a value for the same parameter

Default parameter: Formal parameters are assigned values when they are defined

It is possible to pass a value or not to pass a value, often need to become a parameter defined as a positional parameter, the change of the smaller parameter is defined as the default parameter (formal parameter)

Note the issue:

1. Assign a value only once at the time of definition

2. The definition of the default parameter should be to the right of the location parameter

3. Default parameters should usually be defined as immutable types

Variable length parameters:

      1. In cases where the length of an argument is not fixed at the time of definition, a scheme that can receive variable length arguments should be found from the angle of the formal parameter, which is the variable length parameter (formal parameter)
      2. While the arguments are defined by position and by keyword, there should be two solutions for the variable length of these two forms, namely *args,**kwargs

Python3 function One

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.