関数
パラメータと戻り値を持つ再利用可能なコードブロック。
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
コールスタックはアクティベーションレコードを管理します。末尾呼び出し最適化(TCO)はスタックの増加を排除します。クロージャはエンクロージングスコープの変数をキャプチャします。純粋関数には副作用がありません。高階関数は関数を取るか返します。