Home  >  Article  >  Backend Development  >  What are functions in python? How to define and call functions

What are functions in python? How to define and call functions

乌拉乌拉~
乌拉乌拉~Original
2018-08-15 13:51:353043browse

In this article today, we will learn about the functions in python. First, we need to know the definition and implementation method of the function. The functions in python are well organized and can Reused code segments to implement a single or related function. Functions can improve application modularity and code reuse. You already know that Python provides many built-in functions, such as print(). But you can also create your own functions and try function programming in python This is called a user-defined function.

You can define a function with the functions you want. The following are simple rules:

1. The function code block starts with the def keyword, followed by the function Identifier name and parentheses ().

2. Any incoming parameters and independent variables must be placed between parentheses. Parameters can be defined between parentheses.

3. The first line of the function statement can optionally use a documentation string - used to store function descriptions.

4. The function content starts with a colon and is indented.

5.return [expression] Ends the function and optionally returns 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_suite
   return [expression]

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

Next I will Give a small example:

def printme( str ):
   "打印传入的字符串到标准显示设备上"
   print str
   return

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

said above function, then let’s talk about how to call the function:

Defining a function only 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 through another function call or directly from the Python prompt. The following example calls the printme() function:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 定义函数
def printme( str ):
   "打印任何传入的字符串"
   print str;
   return;
 
# 调用函数
printme("我要调用用户自定义函数!");
printme("再次调用同一函数");

The example output by the above example is as follows:

我要调用用户自定义函数!
再次调用同一函数

The above is what I want to explain today, how to define a function and how to call the function, although the above All the knowledge points are given examples, but you still need to try it yourself. After all, if you don't do it yourself, it is just like talking on paper. Hands-on practice is the best way to verify what you have learned. Finally, I hope this article can bring some help to you who are learning python.

For more related knowledge, please visit the Python tutorial column on the php Chinese website.

The above is the detailed content of What are functions in python? How to define and call functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn