def Function_Name(Parameter):
"""
Introduction
"""
Main Code
return
# 定義函式
def sum_three(a,b,c):
'''
This function will sum all the numbers in the parameters.
'''
output = a + b + c
print('sum =',output)
return output
# 呼叫函式
sum_three(3,6,9)
sum = 18
18
def sum_three(a=1,b=2,c=3):
'''
This function will sum all the numbers in the parameters.
'''
output = a + b + c
print('sum =',output)
return output
sum_three(3,6)
sum = 12
12
x = 10
# Function內使用全域變數
def example():
global x
print(x + 5)
example()
15
x = sum_three(3,6,9)
sum = 18
x
18
help(sum_three)
Help on function sum_three in module __main__: sum_three(a=1, b=2, c=3) This function will sum all the numbers in the parameters.
def Celsius_to_Fahrenheit(celsius = 0):
fahrenheit = (celsius * 9/5) + 32
print(celsius, 'Celsius is equivalent to', fahrenheit, 'Fahrenheit')
return fahrenheit
f = Celsius_to_Fahrenheit(40)
40 Celsius is equivalent to 104.0 Fahrenheit
f
104.0
# 加法
def plus(x,y):
p = x + y
return p
# 減法
def minus(x,y):
m = x - y
return m
# 乘法
def multiply(x,y):
mul = x * y
return mul
# 除法
def divide(x,y):
div = x / y
return div
plus(6,3)
9
minus(6,3)
3
multiply(6,3)
18
divide(6,3)
2.0