Home  >  Article  >  Backend Development  >  How to call a custom function in python

How to call a custom function in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-26 14:36:5911390browse

In python, if you want to call a custom function, you must declare it first and then call it. When using a function, as long as you pass the necessary parameters to the function in the form of the function definition, you can call the function to complete the corresponding function or obtain the processing result returned by the function.

How to call a custom function in python

(1) Declaring a function

Use def in python to declare a function. The complete function consists of the function name, parameters And it consists of function implementation statements (function bodies).

In function declarations, indentation should also be used to indicate that statements belong to the function body.

If the function has a return value, you need to use the return statement in the function to return the calculation result. The general form of declaring the function is as follows:

def 788a85521940291aa43ea2004446ddaa (parameter list):

                                                                                                                                                                                        return. Functions that have no return value after return and no return statement will return None.

Example: To implement the function of finding the sum of all numbers in a tuple, the parameter T is the tuple required to be summed, and the result is the accumulator for the sum of the tuples. Finally, the function uses return to add the accumulated result to result. return. The function declaration code is as follows:

def tpl_sum(T):
    result=0
    for i in T:
        result+=i
    return result

When declaring a function in Python, there is no need to declare the return value type of the function, nor the types of parameters.

Related recommendations: "

Python Video Tutorial

"

(2) Calling functions

Calling custom functions and built-in The difference between functions is that the function needs to be defined first. Example: tpl_sum() function definition and call

The above is the detailed content of How to call a custom function in python. 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