Pythonの関数入門
Hugo Bowne-Anderson
Instructor
引数なしの関数を定義する
引数1つの関数を定義する
値を返す関数を定義する
後ほど: 複数引数、複数の戻り値
str()x = str(5)
print(x)
'5'
print(type(x))
<class 'str'>
def square(): # <- 関数ヘッダーnew_value = 4 ** 2 # <- 関数本体 print(new_value)square()
16
def square(value):
new_value = value ** 2
print(new_value)
square(4)
16
square(5)
25
def square(value): new_value = value ** 2 return new_valuenum = square(4) print(num)
16
Docstring は関数の目的を記述します
関数のドキュメントとして機能します
関数ヘッダー直後の行に置きます
三重の二重引用符 """ で囲みます
def square(value):
"""Returns the square of a value."""
new_value = value ** 2
return new_value
Pythonの関数入門