What are the functions in Python? How to define and invoke a function

Source: Internet
Author: User
In this article we'll look at the functions in Python, and first we need to know function definition and implementation method, functions in Pythonis a well-organized, reusable code snippet 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, and try to function programming in PythonThis is called a user-defined function.

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

1. The function code block begins with the DEF keyword, followed by the function identifier name and parentheses ().

2. Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.

3. The first line of the function statement can optionally use the document string-for storing function descriptions.

4. Function contents start with a colon and indent.

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

The syntax can be seen as follows:

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

(by default, parameter values and parameter names are matched in the order defined in the function declaration.) )

Next I'll give you a small example:

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

(This is a simple Python function that takes a string as an incoming parameter and then prints it to the standard display device.) )

It says the function, so here's how to call the function :

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-*-# definition function def printme (str):   "Prints any incoming string" Print   str;   Return # Call Function Printme ("I want to invoke user-defined function!"); Printme ("Call the same function again");

The example output from the above example is as follows:

I want to invoke the user custom Function! Call the same function again
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.