# 定义函数
def greet_user(username):
print(f"Hello {username}")
greet_user("LLC")
# 位置参数和关键字参数
def func1(value1, value2):
print(f"value1 is {value1}, value2 is {value2}")
# 位置参数
func1("abc", 123)
# 关键字参数
func1(value2=456, value1="def")
# 参数默认值
def func2(value = 1):
print(f"value is {value}")
func2(123)
func2()
# 返回值
def GetFullName(firstName, lastName):
fullName = f"{firstName} {lastName}"
return fullName.title()
name = GetFullName("jimi", "hendrix")
print(name) # << Jimi Hendrix
# 参数传递列表的副本
languages = ["python", "c#", "c++", "lua"]
def func3(param):
param.append("java")
func3(languages)
print(languages) # <<['python', 'c#', 'c++', 'lua', 'java']
# 传递副本
languages = ["python", "c#", "c++", "lua"]
func3(languages[:])
print(languages) # <<['python', 'c#', 'c++', 'lua']
第八章 函数
最新推荐文章于 2024-08-23 16:53:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)