Python learns the "fourth" Python function

Source: Internet
Author: User

I. Introduction to Functions

Functions are well-organized, reusable pieces of code that are used to implement a single, or associated function.

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

Benefits of the function:

    • Code reuse
    • Consistency and ease of maintenance
    • Scalability
Second, the definition of function

Definition rules for functions:

    • function code block starts with def keyword, followed by function identifier name and parentheses ()
    • Any incoming parameters and arguments must be placed in the middle of the parentheses, and the parentheses can be used to define the parameters
    • The first line of the function statement can optionally use the document string-for storing function descriptions
    • Function contents start with a colon, and indent
    • return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None

Syntax for the function:

  

def function Name:    function Body

  

Examples of functions:

Def Print_hello (): "" "    print Hello    : return:" "" "    print (" Hello ")

  

Third, the return value of the function

Look at the return value of Python based on the example:

Def fun2 ():    msg = "Hello World"    return Msgdef fun3 ():    return 1, 2, 3 AA = fun1 () BB = fun2 () cc = FUN3 () print (AA ) print (BB) print (cc) # output: # none# Hello world# (1, 2, 3)

Summarize:

1. If no return statement is returned in the function, the Python function returns none by default

2. The function returns a value of 0, the function returns none by default, the function returns a value of 1 is, the object is returned, and the number of returned value is greater than 1 o'clock, a tuple is returned;

Iv. parameters of the function

Python learns the "fourth" Python 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.