# 函數,即方法,提高代碼的復用性
# 函數的聲明
# 無參數無返回值函數,其實默認返回為空
def hello():
print("hello")
# 有默認參數,有返回值函數
def welcome(name,country ="china"):
return name,country
# 無參數,直接返回函數
def test():
return
r = hello()
# print("沒寫return",r)? #沒寫return None
# print('return多個值得時候',welcome('小黑')) #return多個值得時候 ('小黑', 'china')
# print('return 后邊什么也不寫的時候',test()) #return 后邊什么也不寫的時候 None
# 變量,函數里邊定義的變量是局部變量,只在函數內部生效,
# 如果想全局使用,則需要定義全局變量,即變量前加global
# 函數參數分為
# 必填參數(位置參數)
# 默認值參數
# 可選參數,參數組
def send_msg(*phones):#可選參數,接收到的是一個元組
? ? print(phones)
# 關鍵字參數
def send_sms(**phones):#關鍵字參數,接收到的是一個字典
? ? print(phones)
# send_msg() #()
# send_msg(110) #(110,)
# send_msg(110,120) #(110, 120)
# send_msg(110,120,120,1330000) #(110, 120, 120, 1330000)
send_sms()#{}
send_sms(a=1,b=2,name='abc')#{'a': 1, 'b': 2, 'name': 'abc'}