Python 04 -Function

KjjeddΒ·2025λ…„ 12μ›” 29일

Python

λͺ©λ‘ 보기
4/10
post-thumbnail

πŸ“¦ ν•¨μˆ˜μ™€ λͺ¨λ“ˆ

μ§€κΈˆκΉŒμ§€ μš°λ¦¬λŠ” μœ„μ—μ„œ μ•„λž˜λ‘œ ν˜λŸ¬κ°€λŠ” μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ™”λ‹€.
ν•˜μ§€λ§Œ μ½”λ“œκ°€ κΈΈμ–΄μ§ˆμˆ˜λ‘, 반볡이 λ§Žμ•„μ§ˆμˆ˜λ‘
관리할 수 μ—†λŠ” μˆœκ°„μ΄ λ°˜λ“œμ‹œ μ˜¨λ‹€.

이 문제λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•œ 핡심 도ꡬ가 λ°”λ‘œ ν•¨μˆ˜(Function)λ‹€.


πŸ€” μ™œ ν•¨μˆ˜λ₯Ό λ°°μ›Œμ•Ό ν• κΉŒ?

μ—¬λŸ¬ ν•™μƒμ—κ²Œ 같은 인사말을 좜λ ₯ν•˜λŠ” 상황을 μƒκ°ν•΄λ³΄μž.

print("=" * 20)
print("μ•ˆλ…•ν•˜μ„Έμš”, μ² μˆ˜λ‹˜!")
print("μ˜€λŠ˜λ„ 쒋은 ν•˜λ£¨ λ˜μ„Έμš”!")
print("=" * 20)

print("=" 20)
print("μ•ˆλ…•ν•˜μ„Έμš”, μ˜ν¬λ‹˜!")
print("μ˜€λŠ˜λ„ 쒋은 ν•˜λ£¨ λ˜μ„Έμš”!")
print("="
20)

❌ 학생이 100λͺ…이면 같은 μ½”λ“œλ₯Ό 100번 볡사해야 ν•œλ‹€.
❌ 문ꡬλ₯Ό λ°”κΎΈλ €λ©΄ 100ꡰ데λ₯Ό μˆ˜μ •ν•΄μ•Ό ν•œλ‹€.


βœ… ν•΄κ²°μ±…: ν•¨μˆ˜λ‘œ μž¬μ‚¬μš©

def greet(name):
    """ν•™μƒμ—κ²Œ μΈμ‚¬ν•˜λŠ” ν•¨μˆ˜"""
    print("=" * 20)
    print(f"μ•ˆλ…•ν•˜μ„Έμš”, {name}λ‹˜!")
    print("μ˜€λŠ˜λ„ 쒋은 ν•˜λ£¨ λ˜μ„Έμš”!")
    print("=" * 20)

greet("철수")
greet("영희")
greet("민수")

이제 인사말을 λ°”κΎΈκ³  μ‹Άλ‹€λ©΄ ν•¨μˆ˜ ν•˜λ‚˜λ§Œ μˆ˜μ •ν•˜λ©΄ λœλ‹€.


πŸ“ˆ ν•¨μˆ˜μ˜ μž₯점

μž₯점 μ„€λͺ…
μ½”λ“œ μž¬μ‚¬μš© ν•œ 번 μž‘μ„±ν•œ λ‘œμ§μ„ μ—¬λŸ¬ 번 μ‚¬μš©
가독성 의미 μžˆλŠ” μ΄λ¦„μœΌλ‘œ μ½”λ“œ μ˜λ„ ν‘œν˜„
μœ μ§€λ³΄μˆ˜ μˆ˜μ •μ€ ν•œ κ³³μ—μ„œλ§Œ
ν˜‘μ—… κΈ°λŠ₯ λ‹¨μœ„λ‘œ μ—­ν•  뢄리

πŸ₯€ ν•¨μˆ˜ = 자판기

  • μž…λ ₯(Input) : 돈, λ²„νŠΌ
  • 처리(Process) : λ‚΄λΆ€ 둜직
  • 좜λ ₯(Output) : 음료
def vending_machine(money, button):
    if money >= 1000 and button == "콜라":
        return "콜라"
    elif money >= 1500 and button == "컀피":
        return "컀피"
    else:
        return "μž”μ•‘ λΆ€μ‘±"

drink = vending_machine(1500, "컀피")
print(drink)


🧱 ν•¨μˆ˜μ˜ κΈ°λ³Έ ꡬ쑰

def ν•¨μˆ˜μ΄λ¦„(λ§€κ°œλ³€μˆ˜):
    μ‹€ν–‰ν•  μ½”λ“œ
    return λ°˜ν™˜κ°’
  • λ§€κ°œλ³€μˆ˜(Parameter) : ν•¨μˆ˜ μ •μ˜ μ‹œ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜
  • 인자(Argument) : ν•¨μˆ˜ 호좜 μ‹œ μ „λ‹¬ν•˜λŠ” μ‹€μ œ κ°’

πŸ”„ λ‹€μ–‘ν•œ ν•¨μˆ˜ ν˜•νƒœ

def say_hello():
    print("μ•ˆλ…•ν•˜μ„Έμš”")

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

def get_min_max(numbers):
return min(numbers), max(numbers)

return이 μ—†μœΌλ©΄ ν•¨μˆ˜λŠ” μžλ™μœΌλ‘œ None을 λ°˜ν™˜ν•œλ‹€.


⚠️ return의 μ€‘μš”ν•œ νŠΉμ§•

  • return을 λ§Œλ‚˜λ©΄ ν•¨μˆ˜λŠ” μ¦‰μ‹œ μ’…λ£Œλœλ‹€
  • return 이후 μ½”λ“œλŠ” μ‹€ν–‰λ˜μ§€ μ•ŠλŠ”λ‹€

🧰 λ‚΄μž₯ ν•¨μˆ˜ (Built-in Functions)

ν•¨μˆ˜ μ—­ν• 
len()길이
sum()합계
max(), min()μ΅œλŒ€/μ΅œμ†Œ
sorted()μ •λ ¬

🧠 λ§€κ°œλ³€μˆ˜ 심화

κΈ°λ³Έκ°’ λ§€κ°œλ³€μˆ˜

def greet(name, greeting="μ•ˆλ…•ν•˜μ„Έμš”"):
    print(f"{greeting}, {name}λ‹˜!")

❗ κΈ°λ³Έκ°’ λ§€κ°œλ³€μˆ˜λŠ” λ°˜λ“œμ‹œ 뒀에 μœ„μΉ˜ν•΄μ•Ό ν•œλ‹€.


🏷 ν‚€μ›Œλ“œ 인자

def introduce(name, age, city):
    print(f"{city}에 μ‚¬λŠ” {age}μ‚΄ {name}")

introduce(age=22, city="λΆ€μ‚°", name="영희")


🌍 λ³€μˆ˜μ˜ λ²”μœ„ (Scope)

  • μ§€μ—­ λ³€μˆ˜: ν•¨μˆ˜ λ‚΄λΆ€
  • μ „μ—­ λ³€μˆ˜: ν•¨μˆ˜ μ™ΈλΆ€
message = "μ „μ—­"

def test():
message = "μ§€μ—­"
print(message)

test()
print(message)


πŸ” LEGB κ·œμΉ™

λ³€μˆ˜λ₯Ό μ°ΎλŠ” μˆœμ„œ:

  1. Local
  2. Enclosing
  3. Global
  4. Built-in

🚨 global ν‚€μ›Œλ“œ 주의

μ „μ—­ λ³€μˆ˜λ₯Ό ν•¨μˆ˜ μ•ˆμ—μ„œ μˆ˜μ •ν•˜λ €λ©΄ global이 ν•„μš”ν•˜μ§€λ§Œ,
μ‹€λ¬΄μ—μ„œλŠ” 거의 μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.

λŒ€μ‹  인자둜 λ°›κ³  return으둜 λ°˜ν™˜ν•˜λŠ” ꡬ쑰가 μ•ˆμ „ν•˜λ‹€.


πŸ“„ λ…μŠ€νŠΈλ§ (Docstring)

def calculate_bmi(weight, height):
    """
    BMIλ₯Ό κ³„μ‚°ν•©λ‹ˆλ‹€.
Args:
    weight (float): 체쀑
    height (float): ν‚€

Returns:
    float: BMI κ°’
"""
return weight / (height ** 2)

λ…μŠ€νŠΈλ§μ€ ν˜‘μ—…κ³Ό μœ μ§€λ³΄μˆ˜μ˜ 기본이닀.


βœ… 핡심 μš”μ•½

  • ν•¨μˆ˜λŠ” μ½”λ“œ μž¬μ‚¬μš©μ˜ 핡심이닀
  • 쒋은 ν•¨μˆ˜ 이름은 μ½”λ“œ μ„€λͺ…이닀
  • return은 ν•¨μˆ˜μ˜ μΆœκ΅¬λ‹€
  • μ „μ—­ λ³€μˆ˜λ³΄λ‹€ 인자/λ°˜ν™˜μ„ μ‚¬μš©ν•˜μž
  • λ…μŠ€νŠΈλ§μ€ 미래의 λ‚˜μ™€ λ™λ£Œλ₯Ό μœ„ν•œ λ°°λ €λ‹€

profile
Gongbuhaja

0개의 λŒ“κΈ€