table of Contents
function
Benefits function: function is more convenient, multiplexing, a plurality of scenes can be used in
Function is used to: implement a performance function understood as a tool, we encountered problems to make use of this tool.
Direct function calls
def get_pi():
import random
count = 0
for i in range(10000):
x, y = random.random(), random.random()
dist = pow((x - 0) ** 2 + (y - 0) ** 2, 0.5)
if dist < 1:
count += 1
print(4 * count / 10000)
#
get_pi()
Function calls with arguments
def get_pi(num):
import random
count = 0
for i in range(num):
x, y = random.random(), random.random()
dist = pow((x - 0) ** 2 + (y - 0) ** 2, 0.5)
if dist < 1:
count += 1
print(4 * count / num)
get_pi(10)
get_pi(100)
get_pi(1000)
get_pi(10000)
get_pi(100000)
With the return value of a function call
def add_sum(num):
count = 0
for i in range(1, num):
count += i
return count
res1= add_sum(101)
print('res1:',res1)
res2 = add_sum(1001)
print('res2:',res2)
res3 = add_sum(10001)
print('res3:',res3)
Author: bean paste pubescens
out at: https://www.cnblogs.com/chenziqing/
view other blog, please click: https://www.cnblogs.com/chenziqing/
solidarity with the blogger: If you think the article helpful , you can click on the bottom right corner of the article [recommended] it. Your encouragement is the greatest power bloggers!