본문으로 건너뛰기
Previous
Next
Python Functions | Parameters· Return Values

Python Functions | Parameters· Return Values

Python Functions | Parameters· Return Values

이 글의 핵심

Learn Python functions: *args/**kwargs, default arguments, lambdas, closures, decorators, recursion, and typing— with examples and pitfalls.

Introduction: Functions as the basic unit of code

Functions are reusable blocks of work. In Python, functions are first-class objects: you can assign them to variables and pass them as arguments.

1. Function basics

Defining and calling functions

def greet(name):
    """
    Return a greeting for the given name.
    Args:
        name (str): Person to greet
    Returns:
        str: Greeting message
    """
    return f"Hello, {name}!"
print(greet("Bob"))

Multiple return values (tuples)

def get_user_info():
    name = "Bob"
    age = 25
    city = "Seoul"
    return name, age, city
name, age, city = get_user_info()
name, _, city = get_user_info()

Functions with no return value

def print_hello():
    print("Hello")
result = print_hello()
print(result)
print(type(result))

None patterns

def find_user(user_id):
    users = {1: "Bob", 2: "Alice"}
    return users.get(user_id)
user = find_user(5)
if user is None:
    print("Not found")

2. Parameters

Positional arguments

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

Keyword arguments

def introduce(name, age, city):
    print(f"{name} ({age}) — {city}")
introduce("Bob", 25, "Seoul")
introduce(age=25, name="Bob", city="Seoul")
introduce("Bob", age=28, city="Daejeon")

Default arguments

def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"
print(greet("Bob"))
print(greet("Bob", "Hi"))

Never use a mutable default:

def add_item_bad(item, items=[]):
    items.append(item)
    return items
def add_item_safe(item, items=None):
    if items is None:
        items = []
    items.append(item)
    return items

*args and **kwargs

def sum_all(*numbers):
    return sum(numbers)
def print_info(**kwargs):
    for k, v in kwargs.items():
        print(f"{k}: {v}")
def complex_func(a, b, *args, **kwargs):
    print(a, b, args, kwargs)
complex_func(1, 2, 3, 4, x=5, y=6)

Parameter order (typical): positional → *args → defaults (careful with Python 3.8+ keyword-only) → **kwargs.

3. Lambda functions

square = lambda x: x ** 2
print(square(5))
students = [("Bob", 85), ("Alice", 90), ("Carol", 80)]
print(sorted(students, key=lambda x: x[1], reverse=True))
numbers = [1, 2, 3, 4, 5]
print(list(map(lambda x: x ** 2, numbers)))
print(list(filter(lambda x: x % 2 == 0, numbers)))

Prefer list comprehensions over map/filter when readability wins.

4. Closures

def make_multiplier(n):
    def multiply(x):
        return x * n
    return multiply
times_3 = make_multiplier(3)
print(times_3(10))

nonlocal

def make_counter():
    count = 0
    def increment():
        nonlocal count
        count += 1
        return count
    return increment

5. Decorators (introduction)

Decorators wrap functions to add behavior (logging, timing, auth) without editing the original function’s body.

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("before")
        result = func(*args, **kwargs)
        print("after")
        return result
    return wrapper
@my_decorator
def add(a, b):
    return a + b

Examples: timing and caching

import time
from functools import lru_cache
def measure_time(func):
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        print(f"{func.__name__}: {time.time() - start:.4f}s")
        return result
    return wrapper
@lru_cache(maxsize=128)
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n - 1) + fibonacci(n - 2)

6. Recursion and higher-order functions

def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)
def apply_operation(func, value):
    return func(value)

Best practices

  • Clear names, single responsibility, docstrings.
  • Avoid mutable defaults; use None and create a new list/dict inside.
  • Type hints (typing) help IDEs and mypy.
  • Prefer pure functions when possible.

Summary

  1. Define with def, return with return (implicit None if omitted).
  2. Positional, keyword, defaults, *args, **kwargs.
  3. Lambdas for tiny expressions; comprehensions often clearer than map/filter.
  4. Closures + nonlocal for enclosed state.
  5. Decorators compose behavior; use functools.wraps in production (see advanced post).

Next steps


Keywords (SEO)

Python functions, def, parameters, return, lambda, closure, decorator, *args, **kwargs, higher-order functions, recursion, type hints.

  • [Python syntax basics](/en/blog/python-series-02-basic-syntax/
  • [Python data types](/en/blog/python-series-03-data-types/
  • [Python classes](/en/blog/python-series-05-classes/
  • Python exception handling

자주 묻는 질문 (FAQ)

Q. 이 내용을 실무에서 언제 쓰나요?

A. Learn Python functions: *args/kwargs, default arguments, lambdas, closures, decorators, recursion, and typing— with ex… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.

Q. 선행으로 읽으면 좋은 글은?

A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. Python 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.

Q. 더 깊이 공부하려면?

A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.


같이 보면 좋은 글 (내부 링크)

이 주제와 연결되는 다른 글입니다.

  • [Python Classes | Object-Oriented Programming (OOP) Explained](/en/blog/python-series-05-classes/
  • [Arrays and Lists](/en/blog/algorithm-series-01-array-list/
  • [JavaScript Functions | Declarations· Arrows](/en/blog/javascript-series-03-functions/

이 글에서 다루는 키워드 (관련 검색어)

Python, Functions, Lambda, Decorator, args, kwargs, Beginner 등으로 검색하시면 이 글이 도움이 됩니다.