假設我想要一杯紅茶微糖去冰...
def 函式名稱(參數):
函式要做的事情
return 回傳值
def say_hi_to(name):
print('Hi, '+name+'!')
return True;
函式名稱(參數)
greet = False
greet = say_hi_to('Peter')
#'Hi, Peter!'
#greet = True
def say_hi():
print('Hi~')
def say_hi_to(name):
print('Hi, ' + name + '!')
def say_sth_to(name, greet):
print(greet + ', ' + name + '!')
say_hi() #Hi~
my_name = 'Python'
say_hi_to(my_name) #Hi, Python!
my_name = 'World'
say_sth_to(my_name, 'Hello') #Hello, World!
def mutable_or_immutable(para):
para += '1'
a = 'a'
b = ['b']
mutable_or_immutable(a)
print(a) #a
mutable_or_immutable(b)
print(b) #['b','1']
在函式中對參數做更動時:
使用時務必謹慎!
def function1():
print('no return')
def function2():
print('no return')
return
def function3():
print('return bool')
return True;
def function4():
print('return tuple')
return (1,2)
def function5():
print('return tuple')
return 1,2
華氏 = 攝氏*(9/5)+32
攝氏 = (華氏-32)*5/9
輸入C結束程式