Eighth Python custom Function return value

Source: Internet
Author: User

Note Custom functions: function names within a unified file cannot be the same.

Single return value

Syntax structure:

def function_name ([para1,para2 ...])

Code ...

Code ...

...

Return value--------------A value can be returned

eg

def test_c (n1,n2):-------------function definition
N=n1+n2
return n

Sum1=test_c (100,102)----------function call----It is best not to use sum because the sum is the keyword
print ' sum1= ', sum1

The sun1 is used to accept the return value of a function. The custom function has a return value, and a variable to hold or accept the return value when called

Multiple return values

eg

Syntax structure:

def function_name ([para1,para2 ...])

Code ...

Code ...

...

Return Value1,value2,value3:--------------can return multiple values, comma interval

eg

def test_d (N1,N2):
N=n1+n2
M=n1*n2
P=n1-n2
E=n1**n2
Return n,m,p,e

Sum1,multi,plus,pow=test_d (2,10)-----------preferably in this form
Print Sum1,multi,plus,pow

You must have the same number of variables to hold the return value of the function.

If the keynote function has only one variable, the amount of change is stored in tuples.

eg

Re=test_d (2,10)

Print RE

The results are as follows:

(12,20,-8,1024)-----parentheses surround the tuple

Print re[0],re[1],re[2],re[3]---------------elements used to print tuples



Eighth Python custom Function return 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.