Read through the Python custom function and the Python function return value, with a detailed example

Source: Internet
Author: User
function is one of the core contents of Python programming. In this article, we mainly introduce the following Python custom FunctionsThe concept and python function return valueRelevant knowledge points. Python custom FunctionsWhat is it? What it does, how to define a function, and how to call python function return value

What is a python custom function

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.

So how to customize the python function

1. You can define a function that you want to function, the following is a simple rule:

2. The function code block begins with the 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 function descriptions.

5. Function contents start with a colon and indent.

6.return [expression] End function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.

About the python custom function syntax :

def functionname (parameters): "Function _ Document String" Function_suitereturn [expression]

By default, parameter values and parameter names are matched in the order defined in the function declaration.

Instance

def printme (str): "Print incoming string to standard display device" Print Strreturn

So how do I write a python function return value?

Return statement:

Return statement [expression] exits the function, optionally returning an expression to the caller. A return statement without a parameter value returns none. The previous example does not demonstrate how to return a value, and the following example tells you how to do it:

#!/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 result of the above example output:

Inside function:  30

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.