Python Basics-Functions

Source: Internet
Author: User

Python 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.

Define a function

You can define a function that you want to function, and here are the simple rules:

    • The function code block begins with a def keyword followed by the function identifier name and parentheses ().
    • Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
    • The first line of the function statement can optionally use the document string-for storing the function description.
    • The function contents begin with a colon and are indented.
    • return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
Grammar
def functionname (Parameters):    " Function _ Document string "    # functionname  function name          function_suite   return [expression]
View Code

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

Function call

Defines a function that gives the function a name, specifies the parameters contained in the function, and the code block structure.

Once the basic structure of this function is complete, you can execute it from another function call or directly from the Python prompt.

The following instance calls the Printme () function:

#!/usr/bin/python#-*-coding:utf-8-*- #Defining Functionsdefprintme (str):"print any passed-in string"   Printstr; return; #calling FunctionsPrintme"I want to invoke the user custom Function!");p Rintme ("call the same function again");

Python Basics-Functions

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.