{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ch04-title",
   "metadata": {},
   "source": [
    "# Functions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch04-overview",
   "metadata": {},
   "source": [
    "Functions let you name a reusable block of code and call it whenever you need that behavior.\n",
    "\n",
    "- Functions reduce repetition by giving a name to a reusable process\n",
    "- Parameters let a function work with different inputs\n",
    "- Return values let a function produce a result for later code to use\n",
    "- Scope controls where names are available\n",
    "- Recursion solves a problem by reducing it to smaller versions of itself\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "video",
   "metadata": {},
   "source": [
    "<h2>Chapter Video</h2>\n",
    "\n",
    "This short overview introduces Python functions and the basic idea of defining reusable code with `def`.\n",
    "\n",
    "```{raw} html\n",
    "<iframe width=\"100%\" height=\"400\" src=\"https://www.youtube-nocookie.com/embed/9Os0o3wzS_I\" title=\"Python Tutorial for Beginners 8: Functions\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen referrerpolicy=\"strict-origin-when-cross-origin\"></iframe>\n",
    "```\n",
    "\n",
    "> *[Python Tutorial for Beginners 8: Functions (21:47)](https://youtube.com/watch?v=9Os0o3wzS_I) by Corey Schafer*\n",
    ">\n",
    "> Hosted on YouTube; property of its original creator, embedded here for educational reference only."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch04-goals",
   "metadata": {},
   "source": [
    "<h2>Learning Objectives</h2>\n",
    "\n",
    "By the end of this chapter, you will be able to:\n",
    "\n",
    "1. Define and call custom functions with `def`\n",
    "2. Use parameters, arguments, defaults, `*args`, and `**kwargs`\n",
    "3. Use `return` statements to send results back to the caller\n",
    "4. Explain local and global scope, function composition, pure functions and side effects, docstrings, and basic lambda expressions\n",
    "5. Write closures with nested functions, enclosing scope, and `nonlocal`\n",
    "6. Trace recursive function calls using base cases and recursive cases\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ch04-flow",
   "metadata": {},
   "source": [
    "<h2>Chapter Flow</h2>\n",
    "\n",
    "```{container} chapter-flow\n",
    "1. {doc}`0401-functions`\n",
    "2. {doc}`0402-function-design`\n",
    "3. {doc}`0403-recursion`\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "glossary",
   "metadata": {},
   "source": [
    "<h2>Glossary</h2>\n",
    "\n",
    "| # | Term | Explanation |\n",
    "|---:|---|---|\n",
    "| 1 | Function | A named, reusable block of code that performs a task. |\n",
    "| 2 | Function definition | Code that creates a function using `def`, a name, parameters, and a body. |\n",
    "| 3 | Function call | An expression that runs a function by using its name followed by parentheses. |\n",
    "| 4 | Function body | The indented block of statements that runs when a function is called. |\n",
    "| 5 | Parameter | A variable name listed in a function definition. |\n",
    "| 6 | Argument | A value passed into a function call. |\n",
    "| 7 | Default parameter | A parameter that has a fallback value when the caller does not provide an argument. |\n",
    "| 8 | `*args` | Syntax that collects extra positional arguments into a tuple. |\n",
    "| 9 | `**kwargs` | Syntax that collects extra keyword arguments into a dictionary. |\n",
    "| 10 | Return value | The result a function sends back to the caller. |\n",
    "| 11 | Return statement | A statement that exits a function and optionally sends a value back. |\n",
    "| 12 | Type annotation | A note in code that records the expected type of a parameter or return value. |\n",
    "| 13 | Scope | The region of a program where a name can be used. |\n",
    "| 14 | Local variable | A variable created inside a function and available only within that function call. |\n",
    "| 15 | Function composition | Building a result by using one function's output as another function's input. |\n",
    "| 16 | Docstring | A string that documents what a function does. |\n",
    "| 17 | Lambda function | A small anonymous function written as a single expression. |\n",
    "| 18 | Recursion | A pattern where a function calls itself. |\n",
    "| 19 | Stack diagram | A diagram that shows active function calls and their local variables. |\n",
    "| 20 | Infinite recursion | Recursion that never reaches a stopping condition. |\n",
    "| 21 | Pure function | A function whose result depends only on its inputs and that does not change outside state. |\n",
    "| 22 | Side effect | A change or interaction outside a function's return value, such as printing or mutating a list. |\n",
    "| 23 | Base case | The branch of a recursive function that stops the recursion. |\n",
    "| 24 | Recursive case | The branch of a recursive function that calls the function again on a smaller or simpler input. |\n",
    "| 25 | Memoization | Storing previous results so repeated function calls can reuse them. |\n",
    "| 26 | Nested function | A function defined inside another function. |\n",
    "| 27 | Enclosing scope | The local scope of an outer function, which a nested function can read. |\n",
    "| 28 | Closure | A function that remembers values from the scope where it was created, even after that scope has finished. |\n",
    "| 29 | Function factory | A function that builds and returns new, configured functions. |\n",
    "| 30 | `nonlocal` | A keyword that lets a nested function assign to a variable in its enclosing scope. |\n",
    "| 31 | Global variable | A variable defined at the top level of a program, outside any function, that code throughout the module can read. |\n",
    "| 32 | `global` | A keyword that lets a function assign to a global variable instead of creating a new local one. |\n",
    "| 33 | Keyword argument | An argument passed by parameter name, such as `greet(name=\"Ana\")`, so its position does not matter. |\n",
    "| 34 | Boolean function | A function that returns `True` or `False`, often used as the condition of an `if` statement. |\n",
    "| 35 | Dead code | Code that can never run, such as a statement that comes after a `return` on every path through the function. |\n",
    "| 36 | Stack frame | The record of one active function call and its local variables; a stack diagram shows one frame per call. |\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0400-slides",
   "metadata": {},
   "source": [
    "<h2><a href=\"overview.html\" target=\"_blank\" style=\"color: var(--pst-color-link, #176de8);\">Chapter Overview Slides</a></h2>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
