What is the return statement? What role does the return statement play?

Source: Internet
Author: User
Article we'll look at the return statement in Python. First we need to understand the use of the return statement, and then we know what the return statement is used for , and what kind of knowledge is behind the return statement . Without saying much, let's get to the point and get to know the return statement in Python.

Return statement:return statement [expression] exits the function, optionally returning an expression to the caller. A return statement without a parameter value returns none. Return is simply the return of the value in the function to the place where the statement was called, but just a return, usage is too much too flexible in the main function, can be used as a function of the end of the flag, in the calling function can use to return the required value, can also be used to flag whether the program run satisfies a certain condition.

Let's give an example as follows:

#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def sum (arg1, arg2):   # Returns the sum of 2 parameters. "   Total = arg1 + arg2   print "in function:"   ; # Call the sum function total = SUM (10, 20);

The above example outputs the results as follows:

Inside function:  30

The return statement is a value returned by a function in the Python language, and each function should have a return value, where the return value can be a numeric value, a string, a Boolean value, or a list. The Python function returns the value return, the function must have return return value is the complete function, if you do not define the Python function return value, then get a result is a None object, and none means that there is no value.

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.