{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "505b4e39",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Working with Lists in Functions\n",
    "#### CS 65/STEM 280: Introduction to Computer Science I"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd6f0f8f",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Lists as arguments and return values\n",
    "\n",
    "You can normally use lists as arguments and return values just fine, though there's a slight nuance to be aware of. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b40275d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "def double_val(param):\n",
    "    param = param * 2\n",
    "\n",
    "x = 4\n",
    "double_val(x)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af4b825a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8]\n"
     ]
    }
   ],
   "source": [
    "def double_list(param):\n",
    "    param[0] = param[0] * 2\n",
    "\n",
    "x = [4]\n",
    "double_list(x)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "540e9100",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "## Why does this happen?\n",
    "\n",
    "Big objects like collections are really stored in a different part of memory which is _referenced_ by the variable name.\n",
    "\n",
    "When you pass one of these objects as an argument, the parameter is really a copy of the reference, _not the value itself_.\n",
    "\n",
    "Small objects like ints and floats really do get copied to the parameter."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "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.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
