{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5841d5c6-8ade-4e90-9134-a56f0b1faace",
   "metadata": {},
   "source": [
    "## Lecture 13, 29 September 2026"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec9c6118-242c-4572-bfe9-caceaa4db493",
   "metadata": {},
   "source": [
    "### Default values for function parameters\n",
    "- Can provide a default value for a parameter\n",
    "- If no argument is passed, default value is used\n",
    "- Recall `int(s)` converts a string `s` into an `int` if it makes sense"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d4ef37a1-af0a-4830-9319-513138106b62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"7\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "064b9b0b-525c-49a7-99bd-0a32d92f75a2",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'AB'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mValueError\u001b[39m                                Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m int(\u001b[33m\"AB\"\u001b[39m)\n",
      "\u001b[31mValueError\u001b[39m: invalid literal for int() with base 10: 'AB'"
     ]
    }
   ],
   "source": [
    "int(\"AB\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d214542-c606-4fc3-ba02-0ac4eefb74cb",
   "metadata": {},
   "source": [
    "- The error message above refers to `base 10`\n",
    "- `int()` has an optional second parameter, which is the base\n",
    "- If we specify base 16, the previous conversion works"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9bfd9cef-3a67-4191-b9ad-7de7b0317d1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "171"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int(\"AB\",16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "127e553e-6b10-42fc-b2a1-28675af78dd4",
   "metadata": {},
   "source": [
    "- The way such a function is defined is as follows\n",
    "```\n",
    "def int(s,b=10):\n",
    "   ....\n",
    "```\n",
    "- If we call this function with only one argument, the default value of `b` is used, so the conversion is in base 10\n",
    "- If we pass two arguments, the second value is used to set `b`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41fef202-59b0-4d1f-9c37-2ce08cb8e1ce",
   "metadata": {},
   "source": [
    "- Here is another example, to illustrate the point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7cecaea3-0d3c-4347-b516-e4577cf65d95",
   "metadata": {},
   "outputs": [],
   "source": [
    "def checkdef(x = 0, y = 0):\n",
    "    print(\"x:\",x,\"y:\",y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fbb896e9-5f7b-4eda-8de9-52dc5c37d3b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 0 y: 0\n"
     ]
    }
   ],
   "source": [
    "checkdef()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "efd71bc4-08a3-42d6-a0c4-e48d021eb769",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 7 y: 0\n"
     ]
    }
   ],
   "source": [
    "checkdef(7)  # 7 is used for the first parameter x, y gets the default value"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "690fdfc6-b7a2-499f-8b67-0b28a29b1921",
   "metadata": {},
   "source": [
    "- We can also pass parameters by name, ignoring the order"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cd82f198-a4cf-4f8a-a8b0-44cc23f4b7e2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 12 y: 17\n"
     ]
    }
   ],
   "source": [
    "checkdef(y=17,x=12)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "410524ef-0457-406e-9987-6a497c3d7d66",
   "metadata": {},
   "source": [
    "- Using this, we can pass an argument for `y` and use the default `x`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "85f0b5ec-f7d9-4c87-ab18-d46824e8e543",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x: 0 y: 9\n"
     ]
    }
   ],
   "source": [
    "checkdef(y=9)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
