함수

매개변수와 반환 값을 가진 재사용 가능한 코드 블록.

Functions are the primary tool for abstraction and code reuse in software engineering. They allow developers to group a sequence of instructions into a single unit with a name. When a function is called, the program's execution jumps to the function code and then returns to the original point. This promotes the 'Don't Repeat Yourself' (DRY) principle, making code more modular and maintainable. Modern programming paradigms treat functions as 'first-class citizens,' meaning they can be assigned to variables, passed as arguments, and returned from other functions.

        graph LR
  Center["함수"]:::main
  Pre_variable["variable"]:::pre --> Center
  click Pre_variable "/terms/variable"
  Rel_recursion["recursion"]:::related -.-> Center
  click Rel_recursion "/terms/recursion"
  Rel_compiler["compiler"]:::related -.-> Center
  click Rel_compiler "/terms/compiler"
  Rel_computational_neuroscience["computational-neuroscience"]:::related -.-> Center
  click Rel_computational_neuroscience "/terms/computational-neuroscience"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 5살도 이해할 수 있게 설명

함수는 공장에 있는 기계와 같습니다. 무언가(원재료)를 넣으면 기계가 작동하여 무언가(완성된 장난감)가 나옵니다. 같은 기계를 계속 사용할 수 있습니다!

🤓 Expert Deep Dive

호출 스택은 활성화 레코드를 관리합니다. 꼬리 호출 최적화(Tail Call Optimization)는 스택 증가를 방지합니다. 클로저는 둘러싼 범위의 변수를 캡처합니다. 순수 함수는 부작용이 없어 참조 투명성을 보장합니다. 고차 함수는 함수를 인수로 받거나 반환합니다.

🔗 관련 용어

선행 지식:

📚 출처