Defining a Function
ꡬ문
def function_name():
# functions tasks go here
Python
볡μ¬
def λ ν¨μμ μμμ μ리λ ν€μλμ΄λ€. ν¨μμ μ΄λ¦μ snake_case νμμ λ°λ₯΄λ©°, μνν κΈ°λ₯μ μλ¦°λ€.
() μμλ νλΌλ―Έν°κ° λ€μ΄κ°λ€.
: ν¨μ ν€λμ λμ μ리λ ν€μλμ΄λ€.
νΈμΆν λμλ ν¨μμ μ΄λ¦λ§ μ μΌλ©΄ λλ€.
Parameters & Arguments
νλΌλ―Έν°μ arguments μ μ°¨μ΄λ₯Ό μμ보μ
def [ν¨μλͺ
]([parameters]):
functions . . .
print(β. . .β + parameters) # parameters λ ν¨μ μμμ λ³μμ²λΌ μ¬μ©μ΄ κ°λ₯νλ€.
Python
볡μ¬
ν¨μλ₯Ό νΈμΆν λ λ€μ΄κ°λ μΈμλ argument λΌκ³ νλ€.
[ν¨μλͺ
]([argument])
μ΄ argument ν¨μ λ΄μμ λ³μμ²λΌ μ¬μ©μ΄ λ κ²μ΄λ€.
Built in function
function | ꡬ문 | μν |
max | max(λ³μ1, λ³μ2 . . .) | μ΅λκ°μ μ°Ύμμ€λ€. |
min | min(λ³μ1, λ³μ2 . . .) | μ΅μκ°μ μ°Ύμμ€λ€. |
round | round(λ³μ) | λ°μ¬λ¦Όμ ν΄μ€λ€. |
Return
리ν΄κ°μ΄ μ¬λ¬κ°μΈ κ²½μ°λ₯Ό μμ λ‘ λ체νλ€.
[μμ μ½λ]
def top_tourist_locations_italy():
first = "Rome"
second = "Venice"
third = "Florence"
return first, second, third
most_popular1, most_popular2, most_popular3 = top_tourist_locations_italy()
print(most_popular1)
print(most_popular2)
print(most_popular3)
Python
볡μ¬
[κ²°κ³Ό]
Rome
Venice
Florence