Home  >  Article  >  Backend Development  >  How to define functions in python? Introduction to the calling method of python function

How to define functions in python? Introduction to the calling method of python function

不言
不言Original
2018-09-20 16:06:072785browse

What this article brings to you is how to define functions in python? This introduction to the calling method of python functions has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. The concept of function. A function is a code set that organizes code blocks with independent functions into a whole to make it have special functions.

2. The role of functions can be enhanced by using functions. Code reusability improves the efficiency of program writing

3. The use of functions, functions must be created before they can be used. This process is called function definition. After the function is created, it can be used. The use process is called function calling.

Function definition and call:

1) >>> def function name (formal parameter 1, formal parameter 2): # Definition

... Function Body

Function name (actual parameter 1, actual parameter 2) #Call

4. Comments on the function are written below the function definition, using the """content""" method. Where the pycharm function is called, place the mouse and press Ctrl to quickly view the annotation content of the function

5. The scope of function parameters, the ones defined inside the function are called local variables, and the variables outside the function are called global variables, local variables The scope is limited to internal use of the function

>>> def test(a, b):
...     print(a, b)
...
>>> test(1, 2)
1 2
>>> print(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name &#39;a&#39; is not defined

Formal parameters cannot be called from outside

6. Variables outside the function can be called directly inside the function but the contents of the global variable cannot be modified. You can use the global variable name inside the function. Modify after redefinition

1) >>> a = 1
>>> def test(b):
...     print(b)
...     print(a)
...
>>> test(2)
2
1

You can directly call externally defined variables

2) >>> a = 1
>>> def test():
...     a += 1
...     print(a)
...
>>> test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test
UnboundLocalError: local variable &#39;a&#39; referenced before assignment

The function cannot be modified inside the function. The value of the external variable

3)>>> a = 1
>>> def test():
...     global a
...     a += 1
...     print(a)
...
>>> test()

After redeclaring the variable a inside the function, you can modify the value of the variable a

7. The return value of the function, the keyword return of the function in Python, Generate iterator yield return

1) Definition format:

def function name():

function body

return return value

2) Call format:

Variable name = function name()

3) The content after the return statement will no longer be executed

The above is the detailed content of How to define functions in python? Introduction to the calling method of python function. 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