# 如何定义函数？

``````def 函数名(参数):
函数体
``````

## 定义第一个函数

``````def hello():
print('Hello world!')

hello()
``````

``````Hello world!
``````

## 什么是形参实参，什么是返回值？

``````def add1(num1, num2):
result = num1 + num2

return result

x1, x2 = 1, 2
y = add1(x1, x2)
print(y)
``````

``````3
``````

## 函数参数的一些额外用法

``````def add1(num1, num2):
result = num1 + num2

return result

y = add1(num1=1, num2=2)
print(y)
``````

``````3
``````

``````def add2(num1=1, num2=1):
result = num1 + num2

return result

print(y)
``````

``````2
``````

``````def add2(num1=1, num2=2):
result = num1 + num2

return result

print(y)
``````

``````5
``````

``````def add2(num1=1, num2=2):
result = num1 + num2

return result

print(y)
``````

``````4
``````

## 任意传递实参

``````def save(*temp):
return temp

y = save('1', 2, 'abc', True)
print(y)
``````

``````('1', 2, 'abc', True)
``````

``````def student(**temp):
return temp

y = student(name='老王', age=20, high=180, is_student=True)
print(y)
``````

``````{'name': '老王', 'age': 20, 'high': 180, 'is_student': True}
``````

## 附加

``````from aboutadd import add1

y = add1(1, 2)
print(y)
``````

``````3
``````

THE END

)">