本文要介紹python function 的用法

宣告方法 

def function_name (arg0, arg1,....)

function的使用是以def 開頭 後面接function的名稱 (這裡是給function的參數 )

function 的使用 只要給functio_name() 再加參數即可

第一個範例 :用function 印出Hello world

在這範例並沒有使用到參數

注意function應該要return 值 若沒有輸入return,python會return None


def hello_world():
    print 'Hello world'


hello_world()

第二個範例 :用function  印出參數的值

def print_value (a,b): #Create function (arg1,arg2....)
    print a
    print b
print_value('first','second')

第二個範例 :用function  return 參數相加的結果

def print_add(a,b):
    return a + b

print print_add(10,20)

arrow
arrow
    全站熱搜

    yuanann 發表在 痞客邦 留言(1) 人氣()