Python -- function & procedure

The difference between functional programming and procedural programming: a procedure is a function without a return value. A procedure also has a return value in Python 3, which is None

Functions: reuse code, keep code consistent, and make code easier to expand

Procedure definition and call:

1 def func2():
2     """testing2""" # Description of the process
3     print("in the func2")
4 y=func2()
5 print(y)
6 # >>> in the func2
7 # >>> None

Function definition and call:

1 def func1():
2     """testing1""" # Function description
3     print("in the func1")
4     return 0 #End function, return the value that can be received by variable
5     print("Do you think it will print me") 
6 x=func1()
7 print(x)
8 # >>> in the func1
9 # >>> 0

Function return value can be multiple values, and the data type of value can be multiple

1 def func1():
2     """testing1""" # Function description
3     print("in the func1")
4     return 1, ["a", "b"], {"name": "abc"}, "hello"
5 x=func1()
6 print(x)
7 # >>> in the func1
8 # >>> (1, ['a', 'b'], {'name': 'abc'}, 'hello') #tuple

Function returns the memory address of another function when its return value is another function

 1 def func():
 2     print("I`m  func")
 3     return 0
 4 
 5 def func1():
 6     """testing1""" # Function description
 7     print("in the func1")
 8     return func
 9 x=func1()
10 print(x)
11 # >>> in the func1
12 # >>> <function func at 0x00000223A2ADC040>

The simulator calls the log function to print the log to the file (with time):

 1 import  time
 2 def logger():
 3     time_format = '%Y-%m-%d %X' # Define the time format of the output
 4     # strftime() The function receives a time tuple and returns the local time represented by a readable string to output the time in the defined time format
 5     time_current = time.strftime(time_format)
 6     with open('a.txt', 'a+') as f:
 7         f.write('%s end action\n' % time_current)
 8     return 0
 9 
10 def test1():
11     print('in the test1')
12     logger()
13     return 0
14 
15 def test2():
16     print('in the test2')
17     logger()
18     return 0
19 
20 test1()
21 test2()

Tags: Python Programming simulator

Posted on Sun, 24 May 2020 11:23:27 -0400 by kburger