{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "worst-tobago",
   "metadata": {},
   "source": [
    "<!--\n",
    "title: Python Programming: Functions\n",
    "type: lesson\n",
    "duration: \"01:00\"\n",
    "creator: Susi Remondi\n",
    "-->\n",
    "\n",
    "\n",
    "<h1>Python Programming: Functions</h1>\n",
    "\n",
    "\n",
    "<!--\n",
    "\n",
    "## Overview\n",
    "This lesson introduces students to the concept of functions, beginning with regular functions, then parameters, then multiple parameters. It continues with returning values from functions. It ends with a series of You Do exercises.\n",
    "\n",
    "## Learning Objectives\n",
    "In this lesson, students will:\n",
    "- Identify when to use a function.\n",
    "- Create and call a function with arguments.\n",
    "- Return a value from a function.\n",
    "\n",
    "## Duration\n",
    "90 minutes\n",
    "\n",
    "### Note on timing:\n",
    "This functions lesson is designed to roll into the next one; it ends at a logical break to account for the class timing, but they go together.\n",
    "\n",
    "In the 5 day class, this means that the overall function lessons are designed to roll and continue in the next class. The homework doesn't have parameters, so if you don't get there, that's fine. Go as far as you can until the day is over, then pick up where you left off the next class. This lesson ends with a series of You Do exercises. **Student understanding is more important than staying within the timeframe** - the next lesson is flexible with many exercises that can be dropped.\n",
    "\n",
    "\n",
    "## Suggested Agenda\n",
    "\n",
    "| Time | Activity |\n",
    "| --- | --- |\n",
    "| 0:00 - 0:03 | Welcome |\n",
    "| 0:03 - 0:18| Basic Functions |\n",
    "| 0:20 - 0:35 | Parameters |\n",
    "| 0:35 - 0:57 | Returns and Exercises |\n",
    "| 0:57 - 0:60 | Summary |\n",
    "\n",
    "## In Class: Materials\n",
    "- Projector\n",
    "- Internet connection\n",
    "- Python3\n",
    "-->\n",
    "\n",
    "---\n",
    "\n",
    "## Learning Objectives\n",
    "*After this lesson, you will be able to...*\n",
    "\n",
    "- Identify when to use a function.\n",
    "- Create and call a function with arguments.\n",
    "- Return a value from a function.\n",
    "\n",
    "---\n",
    "\n",
    "## Let's Consider a Repetitive program...\n",
    "\n",
    "Consider a program that prints a $5 shipping charge for products on a website:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "worth-tucson",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You've purchased a Hanging Planter.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n",
      "You've purchased a Shell Mirror.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n",
      "You've purchased a Modern Shag Rug.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n"
     ]
    }
   ],
   "source": [
    "print(\"You've purchased a Hanging Planter.\")\n",
    "print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "# 10 minutes later...\n",
    "print(\"You've purchased a Shell Mirror.\")\n",
    "print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "# 5 minutes later...\n",
    "print(\"You've purchased a Modern Shag Rug.\")\n",
    "print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "textile-orange",
   "metadata": {},
   "source": [
    "\n",
    "What if there are 1,000 orders?\n",
    "\n",
    "---\n",
    "\n",
    "## Functions\n",
    "\n",
    "We can write a **function** to print the order.\n",
    "\n",
    "A function is simple — it's a reusable piece of code. We only define it once. Later, we can use its name as a shortcut to run that whole chunk of code.\n",
    "\n",
    "* Functions are defined using the `def` syntax.\n",
    "    * `def` stands for \"define.\"\n",
    "* In this case, we're *defining* a function named 'function_name.'\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "upset-peoples",
   "metadata": {},
   "outputs": [],
   "source": [
    "def function_name():\n",
    "  # What you want the function to do\n",
    "    pass\n",
    "\n",
    "# Call the function by name to run it:\n",
    "function_name()\n",
    "\n",
    "# 10 minutes later...\n",
    "function_name()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "thrown-mainstream",
   "metadata": {},
   "source": [
    "\n",
    "**Protip:**  Don't forget the `()`, and be sure to indent!\n",
    "\n",
    "---\n",
    "\n",
    "## Seeing Functions in  Action\n",
    "\n",
    "So we *define* the function, then we can *call* the function by pairing its name with the parentheses: `print_order()`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "criminal-boost",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You've purchased a Hanging Planter.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n"
     ]
    }
   ],
   "source": [
    "def print_order():\n",
    "    print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "print(\"You've purchased a Hanging Planter.\")\n",
    "print_order()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "athletic-genetics",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "---\n",
    "\n",
    "## Naming a Function\n",
    "\n",
    "What can you name a function?\n",
    "- Anything you'd like.\n",
    "  - But match the intention and the *invocation*!\n",
    "- Using `print_order` is more descriptive.\n",
    "\n",
    "What do you think will happen if you change the function name `print_order` to `finished_order` without updating the invocation?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "opened-treaty",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You've purchased a Hanging Planter.\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'print_order' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-245f774f4c26>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"You've purchased a Hanging Planter.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mprint_order\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'print_order' is not defined"
     ]
    }
   ],
   "source": [
    "del print_order\n",
    "\n",
    "def finished_order():\n",
    "    print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n",
    "    \n",
    "print(\"You've purchased a Hanging Planter.\")\n",
    "print_order()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "second-intranet",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "---\n",
    "\n",
    "## Multi-Line Functions\n",
    "\n",
    "\n",
    "How many lines of code can a function have?\n",
    "- As many lines of code as you'd like!\n",
    "- Just indent each line.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "embedded-plaza",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello!\n",
      "Bonjour!\n"
     ]
    }
   ],
   "source": [
    "def welcome():\n",
    "    print(\"Hello!\")\n",
    "    print(\"Bonjour!\")\n",
    "\n",
    "welcome()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "gorgeous-neutral",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## We Do: Writing a Function\n",
    "\n",
    "Let's make this a little more complicated.\n",
    "\n",
    "Let's write a function together, `high_low`, that prints \"High!\" if a variable `my_number` is greater than 10 and \"Low!\" if it isn't."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "distributed-bargain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "High!\n",
      "Low!\n"
     ]
    }
   ],
   "source": [
    "def high_low():\n",
    "    if my_number > 10:\n",
    "        print(\"High!\")\n",
    "    else:\n",
    "        print(\"Low!\")\n",
    "\n",
    "my_number = 17\n",
    "high_low()\n",
    "\n",
    "my_number = 8\n",
    "high_low()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mexican-hawaiian",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "---\n",
    "\n",
    "## You Do: FizzBuzz\n",
    "\n",
    "This is a *very* common programming question. It's often on job interviews and a buzzword in the industry as a simple but common task to show your understanding.\n",
    "\n",
    "- Write a function that prints the numbers from 1 to 101.\n",
    "- But, for multiples of three, print “Fizz” instead of the number.\n",
    "- For multiples of five, print “Buzz”.\n",
    "- For numbers which are multiples of both three and five, print “FizzBuzz”.\n",
    "- Don't forget to call the function!\n",
    "\n",
    "_Hint: think carefully about the order of your conditionals._"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "happy-swaziland",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "Fizz\n",
      "4\n",
      "Buzz\n",
      "Fizz\n",
      "7\n",
      "8\n",
      "Fizz\n",
      "Buzz\n",
      "11\n",
      "Fizz\n",
      "13\n",
      "14\n",
      "FizzBuzz\n",
      "16\n",
      "17\n",
      "Fizz\n",
      "19\n",
      "Buzz\n",
      "Fizz\n",
      "22\n",
      "23\n",
      "Fizz\n",
      "Buzz\n",
      "26\n",
      "Fizz\n",
      "28\n",
      "29\n",
      "FizzBuzz\n",
      "31\n",
      "32\n",
      "Fizz\n",
      "34\n",
      "Buzz\n",
      "Fizz\n",
      "37\n",
      "38\n",
      "Fizz\n",
      "Buzz\n",
      "41\n",
      "Fizz\n",
      "43\n",
      "44\n",
      "FizzBuzz\n",
      "46\n",
      "47\n",
      "Fizz\n",
      "49\n",
      "Buzz\n",
      "Fizz\n",
      "52\n",
      "53\n",
      "Fizz\n",
      "Buzz\n",
      "56\n",
      "Fizz\n",
      "58\n",
      "59\n",
      "FizzBuzz\n",
      "61\n",
      "62\n",
      "Fizz\n",
      "64\n",
      "Buzz\n",
      "Fizz\n",
      "67\n",
      "68\n",
      "Fizz\n",
      "Buzz\n",
      "71\n",
      "Fizz\n",
      "73\n",
      "74\n",
      "FizzBuzz\n",
      "76\n",
      "77\n",
      "Fizz\n",
      "79\n",
      "Buzz\n",
      "Fizz\n",
      "82\n",
      "83\n",
      "Fizz\n",
      "Buzz\n",
      "86\n",
      "Fizz\n",
      "88\n",
      "89\n",
      "FizzBuzz\n",
      "91\n",
      "92\n",
      "Fizz\n",
      "94\n",
      "Buzz\n",
      "Fizz\n",
      "97\n",
      "98\n",
      "Fizz\n",
      "Buzz\n"
     ]
    }
   ],
   "source": [
    "def fizz_buzz():\n",
    "    for num in range(1,101):\n",
    "        if num % 15 == 0:\n",
    "            print(\"FizzBuzz\")\n",
    "        elif num % 5 == 0:\n",
    "            print(\"Buzz\")\n",
    "        elif num % 3 == 0:\n",
    "            print(\"Fizz\")\n",
    "        else:\n",
    "            print(num)\n",
    "\n",
    "fizz_buzz()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "steady-cream",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "---\n",
    "\n",
    "## Quick Review: Functions\n",
    "\n",
    "Functions are reusable chunks of code. They can have anything in them.\n",
    "\n",
    "* Define functions using the `def` keyword.\n",
    "* A function must be **called** before the code in it will run!\n",
    "* You will recognize function calls by the `()` at the end.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "becoming-entrance",
   "metadata": {},
   "outputs": [],
   "source": [
    "# This part is the function definition!\n",
    "def say_hello():\n",
    "    print(\"hello world!\")\n",
    "\n",
    "# This part is actually calling/running the function!\n",
    "say_hello()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fleet-buffalo",
   "metadata": {},
   "source": [
    "\n",
    "You can call them as many times as you'd like, but they need to be defined above the code where you call them.\n",
    "\n",
    "Up next: Parameters!\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "## Discussion: Parameters\n",
    "\n",
    "\n",
    "Remember this?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "static-ethernet",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You've purchased a Hanging Planter.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n",
      "You've purchased a Shell Mirror.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n",
      "You've purchased a Modern Shag Rug.\n",
      "Thank you for your order. There will be a $5.00 shipping charge for this order.\n"
     ]
    }
   ],
   "source": [
    "def print_order():\n",
    "    print(\"Thank you for your order. There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "print(\"You've purchased a Hanging Planter.\")\n",
    "print_order()\n",
    "\n",
    "print(\"You've purchased a Shell Mirror.\")\n",
    "print_order()\n",
    "\n",
    "print(\"You've purchased a Modern Shag Rug.\")\n",
    "print_order()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subjective-joint",
   "metadata": {},
   "source": [
    "\n",
    "There's still repetition. How do you think we could improve it?\n",
    "\n",
    "---\n",
    "\n",
    "## Addressing the repetition\n",
    "\n",
    "We can dynamically pass a function values. This is a **parameter**.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "sixth-newport",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thank you for ordering the Hanging Planter.\n",
      "Thank you for ordering the Shell Mirror.\n",
      "Thank you for ordering the Modern Shag Rug.\n"
     ]
    }
   ],
   "source": [
    "def print_order(product):\n",
    "    print(f\"Thank you for ordering the {product}.\")\n",
    "\n",
    "print_order(\"Hanging Planter\")\n",
    "# Prints \"Thank you for ordering the Hanging Planter.\"\n",
    "print_order(\"Shell Mirror\")\n",
    "# Prints \"Thank you for ordering the Shell Mirror.\"\n",
    "print_order(\"Modern Shag Rug\")\n",
    "# Prints \"Thank you for ordering the Modern Shag Rug.\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hired-alert",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Terminology Recap\n",
    "\n",
    "**Parameter:** The variable that's defined in a function's declaration.\n",
    "\n",
    "**Argument:** The actual value passed into the function when the function is called.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "greater-miami",
   "metadata": {},
   "outputs": [],
   "source": [
    "def my_function(parameter):\n",
    "  # Does something.\n",
    "\n",
    "my_function(argument)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "civilian-mailing",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Case of the Missing Argument\n",
    "\n",
    "\n",
    "What happens if you do this incorrectly?\n",
    "\n",
    "Try removing `\"Hanging Planter\"` from the code so `print_order` is called with an empty parentheses. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "wanted-jewel",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "print_order() missing 1 required positional argument: 'product'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-685fd6348a86>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"There will be a $5.00 shipping charge for this order.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint_order\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[0mprint_order\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Shell Mirror\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[0mprint_order\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Modern Shag Rug\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: print_order() missing 1 required positional argument: 'product'"
     ]
    }
   ],
   "source": [
    "def print_order(product):\n",
    "    print(\"Thank you for ordering the\", product, \".\")\n",
    "    print(\"There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "print_order()\n",
    "print_order(\"Shell Mirror\")\n",
    "print_order(\"Modern Shag Rug\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "positive-desperate",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "\n",
    "---\n",
    "\n",
    "## Partner Exercise: Thanks a Latte\n",
    "\n",
    "\n",
    "Pair up! Decide who will be the driver and who will be the navigator.\n",
    "\n",
    "Imagine that you are tasked with creating a program to calculate the total amount, including sales tax, for each item at a coffee shop.\n",
    "\n",
    "Create a new file, `latte.py`, and type the two functions below into it, which will calculate the total amount for two drinks:\n",
    "\n",
    "*Pro tip: Don't just copy! Typing will be good practice.*\n",
    "\n",
    "<aside class=\"notes\">\n",
    "\n",
    "**Teaching Tips**:\n",
    "\n",
    "- Pair the students up for this exercise.\n",
    "- The next slide has code to copy; the slide after has the actual exercise. Make sure students all have the code on the next slide before turning to the subsequent slide.\n",
    "\n",
    "</aside>\n",
    "---\n",
    "\n",
    "## Partner Exercise: Thanks a Latte\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "minus-nursery",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total is $ 6.05\n",
      "The total is $ 5.225\n"
     ]
    }
   ],
   "source": [
    "def latte_total():\n",
    "    price = 5.50\n",
    "    sales_tax_rate = .10\n",
    "    total_amount = price + (price * sales_tax_rate)\n",
    "    print(\"The total is $\", total_amount)\n",
    "\n",
    "latte_total()\n",
    "\n",
    "def americano_total():\n",
    "    price = 4.75\n",
    "    sales_tax_rate = .10\n",
    "    total_amount = price + (price * sales_tax_rate)\n",
    "    print(\"The total is $\", total_amount)\n",
    "\n",
    "americano_total()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "industrial-spectacular",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Keep it DRY (Don't Repeat Yourself)\n",
    "\n",
    "But what if we have several drinks at the coffee shop?\n",
    "\n",
    "- With your partner, create a function called `calculate_price` that accepts one parameter `price`, the price of a drink, and prints the total with sales tax included.\n",
    "- Call your function with arguments `5.5` (the latte) and `4.75` (the americano). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "coordinate-brazilian",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total is $6.05.\n",
      "The total is $5.225.\n"
     ]
    }
   ],
   "source": [
    "def calculate_total(price):\n",
    "    sales_tax_rate = .10\n",
    "    total_amount = price + (price * sales_tax_rate)\n",
    "    print(f\"The total is ${total_amount}.\")\n",
    "\n",
    "calculate_total(5.5) # This will print 6.05.\n",
    "calculate_total(4.75) # This will print 5.225."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "yellow-publisher",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Multiple Parameters: Part 1\n",
    "\n",
    "What about changing sales tax? We can pass as many values into the function as we want - we can have as many parameters as we want.\n",
    "\n",
    "Here, we have a second parameter, `taxes`:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "urban-mattress",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total is $6.05.\n",
      "The total is $5.32.\n"
     ]
    }
   ],
   "source": [
    "def calculate_total(price, taxes):\n",
    "    total_amount = price + (price * taxes)\n",
    "    print(f\"The total is ${total_amount}.\")\n",
    "\n",
    "calculate_total(5.5, .10) # \"price\" is 5.5; \"taxes\" is .10. This will print 6.05.\n",
    "calculate_total(4.75, .12) # \"price\" is 4.75; \"taxes\" is .12. This will print 5.32.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "little-anniversary",
   "metadata": {},
   "source": [
    "\n",
    "**Protip:** Use a comma-separated list — (parameter1, parameter2, parameter3, parameter4)\n",
    "\n",
    "---\n",
    "\n",
    "## Multiple Parameters: Part 2\n",
    "\n",
    "With parameters, order matters! Programs don't automatically understand what should go where - they assign values in order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "casual-analyst",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello Bruce Wayne born in 1939 from Gotham !\n",
      "Hello Bruce 1939 born in Gotham from Wayne !\n"
     ]
    }
   ],
   "source": [
    "def greet_user(firstName, lastName, year, city):\n",
    "    print(\"Hello\", firstName, lastName, \"born in\", year, \"from\", city,  \"!\")\n",
    "\n",
    "greet_user(\"Bruce\", \"Wayne\", 1939, \"Gotham\")\n",
    "greet_user(\"Bruce\", 1939, \"Gotham\", \"Wayne\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "parliamentary-wellington",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Partner Exercise: Functions With Logic\n",
    "\n",
    "\n",
    "With the same partner, switch drivers.\n",
    "\n",
    "Let's go back to our shipping example. Depending on the order amount, our user might get free shipping, so the print statement is different.\n",
    "\n",
    "Use this starter code, which works for one product. Can you build a function from it that works for any `product` and `order_amount`?\n",
    "\n",
    "Test your code with the following products and prices:\n",
    "- Hanging Planter - 35\n",
    "- Shell Mirror - 15\n",
    "- Modern Shag Rug - 75"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "banned-official",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thank you for ordering the Hanging Planter .\n",
      "It's your lucky day! There is no shipping charge for orders over $30.00.\n",
      "Thank you for ordering the Shell Mirror .\n",
      "There will be a $5.00 shipping charge for this order.\n",
      "Thank you for ordering the Modern Shag Rug .\n",
      "It's your lucky day! There is no shipping charge for orders over $30.00.\n"
     ]
    }
   ],
   "source": [
    "def print_order(product, order_amount):\n",
    "    print(\"Thank you for ordering the\", product, \".\")\n",
    "    if order_amount >= 30:\n",
    "        print(\"It's your lucky day! There is no shipping charge for orders over $30.00.\")\n",
    "    else:\n",
    "        print(\"There will be a $5.00 shipping charge for this order.\")\n",
    "\n",
    "print_order(\"Hanging Planter\", 35)\n",
    "print_order(\"Shell Mirror\", 15)\n",
    "print_order(\"Modern Shag Rug\", 75)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "federal-practitioner",
   "metadata": {},
   "source": [
    "* **Hint:** You can put any code you'd like inside a function.\n",
    "* **Reminder:** Don't forget to indent!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deluxe-advocate",
   "metadata": {},
   "source": [
    "\n",
    "</aside>\n",
    "\n",
    "---\n",
    "\n",
    "## Quick Review: Functions with Parameters\n",
    "\n",
    "**Parameter:** The variable that's defined in a function's declaration.\n",
    "\n",
    "**Argument:** The actual value passed into the function when the function is called.\n",
    "\n",
    "Order matters!\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "declared-cannon",
   "metadata": {},
   "outputs": [],
   "source": [
    "def do_something(parameter1, parameter2):\n",
    "  # Does something.\n",
    "\n",
    "do_something(argument1, argument2)\n",
    "do_something(a_different_argument_1, a_different_argument_2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "changing-solid",
   "metadata": {},
   "source": [
    "\n",
    "Next up: Returns.\n",
    "\n",
    "---\n",
    "\n",
    "## The Return\n",
    "\n",
    "Sometimes, we want values *back* from functions.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "senior-supervision",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The total is $ 6.05\n",
      "The total is $ 6.05\n",
      "Your order total is 6.05\n"
     ]
    }
   ],
   "source": [
    "def calculate_total(price, taxes):\n",
    "    total_amount = price + (price * taxes)\n",
    "    print('The total is $', total_amount)\n",
    "    # Send the total_amount for the drink back to the main program.\n",
    "    return total_amount\n",
    "\n",
    "# This just calls the function -  we've seen this.\n",
    "calculate_total(5.5, .10)\n",
    "\n",
    "# This is new! Save the amount of this drink into a variable \"latte_total.\"\n",
    "latte_total = calculate_total(5.5, .10)\n",
    "\n",
    "# Now, we can  use that variable.\n",
    "print('Your order total is', latte_total)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "least-practitioner",
   "metadata": {},
   "source": [
    "\n",
    "* `total_amount` is returned to the main program.\n",
    "\n",
    "* The value in `total_amount` is saved as `latte_total`.\n",
    "\n",
    "* When we **return** something, it ends the function's execution and \"spits out\" whatever we are returning.\n",
    "\n",
    "---\n",
    "\n",
    "## We Do: Practicing Returns\n",
    "\n",
    "Let's fill this in together:\n",
    "\n",
    "- Define a function, `add_two`, that takes a parameter `number`.\n",
    "- It adds `2` to `number`,  saving that in a new variable, `total`; print `total` out. Then, return `total`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "operating-millennium",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "def add_two(number):\n",
    "    total = number + 2\n",
    "    print(total)\n",
    "    return total\n",
    "\n",
    "final_var = add_two(3)\n",
    "print(final_var)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "handled-winner",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Discussion: Return Statements With Logic\n",
    "\n",
    "The `return` statement *exits a function*, not executing any further code in it. What do you think the following will print?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "optical-participation",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "def mystery():\n",
    "    return 6\n",
    "    return 5\n",
    "\n",
    "my_number = mystery()\n",
    "print(my_number)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "restricted-maine",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Discussion: What Will Happen?\n",
    "\n",
    "What do you think will print out?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "nuclear-moses",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65\n"
     ]
    }
   ],
   "source": [
    "def add_bonus_points(score):\n",
    "    if score > 50:\n",
    "        return score + 10\n",
    "    score += 20\n",
    "    return score\n",
    "\n",
    "total_points = add_bonus_points(55)\n",
    "print(total_points)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "elect-circumstances",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Exiting a Function\n",
    "\n",
    "\n",
    "We can also use `return` by itself as a way to exit the function and prevent any code that follows from running.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "public-original",
   "metadata": {},
   "outputs": [],
   "source": [
    "def rock_and_roll(muted):\n",
    "    song = \"It's only Rock 'N' Roll\"\n",
    "    artist = \"Rolling Stones\"\n",
    "\n",
    "    if (muted == True):\n",
    "        return\n",
    "        # Here, we use return as a way to exit a function\n",
    "        # We don't actually return any value.\n",
    "    print(\"Now playing: \", song, \" by \", artist)\n",
    "\n",
    "rock_and_roll(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "graphic-russia",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Now playing:  It's only Rock 'N' Roll  by  Rolling Stones\n"
     ]
    }
   ],
   "source": [
    "rock_and_roll(False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "light-platform",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Quick Knowledge Check\n",
    "\n",
    "Looking at this code, where will the function stop if `x` is `10`?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "satisfied-presence",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def categorize(x):\n",
    "    if (x < 8):\n",
    "        return 8\n",
    "    x += 3\n",
    "    if (x < 15):\n",
    "        return x\n",
    "    return 100\n",
    "\n",
    "categorize(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "amber-preparation",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Another Knowledge Check\n",
    "\n",
    "Take this simple `adder` function:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "clinical-australian",
   "metadata": {},
   "outputs": [],
   "source": [
    "def adder(number1, number2):\n",
    "    return number1 + number2\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eleven-leisure",
   "metadata": {},
   "source": [
    "\n",
    "Which of the following statements will result in an error?\n",
    "\n",
    "A. `adder(10, 100.)` <br>\n",
    "B. `adder(10, '10')` <br>\n",
    "C. `adder(100)` <br>\n",
    "D. `adder('abc', 'def')` <br>\n",
    "E. `adder(10, 20, 30)` <br>\n",
    "\n",
    "Answer:\n",
    "\n",
    "* B. `adder(10, '10')` is incorrect because it tries to combine a string and an integer.\n",
    "\n",
    "* C. `adder(100)` will result in an error because it only provides one value.\n",
    "\n",
    "* E. `adder(10, 20, 30)` provides too many.\n",
    "\n",
    "---\n",
    "\n",
    "## Quick Review: Return Statements\n",
    "\n",
    "Return statements allow us to get values back from functions:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "virtual-sustainability",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_two(number):\n",
    "    total = number + 2\n",
    "    print(total)\n",
    "    return total\n",
    "\n",
    "final_var = add_two(3)\n",
    "print(final_var)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "extended-venture",
   "metadata": {},
   "source": [
    "\n",
    "Return statements also exit the function - no further code in the function happens!\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "corporate-coating",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_bonus_points(score):\n",
    "    if score > 50:\n",
    "        return score + 10\n",
    "    score += 30\n",
    "    return score\n",
    "\n",
    "total_points = add_bonus_points(55)\n",
    "print(total_points) # 65\n",
    "total_points = add_bonus_points(10)\n",
    "print(total_points) # 40"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "legitimate-functionality",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Temperature Conversion\n",
    "\n",
    "When we were learning about conditionals, we took a look at a program that let us know if it was too hot or too cold:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "exciting-partnership",
   "metadata": {},
   "outputs": [],
   "source": [
    "temperature = 308\n",
    "if temperature > 299:\n",
    "    print(\"It's too hot!\")\n",
    "elif temperature <= 299 and temperature > 288:\n",
    "    print(\"It's just right!\")\n",
    "elif temperature <= 288 and temperature > 277:\n",
    "    print(\"It's pretty cold!\")\n",
    "else:\n",
    "    print(\"It's freezing!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "postal-madison",
   "metadata": {},
   "source": [
    "\n",
    "That's good logic, but Kelvins aren't incredibly useful on a day-to-day basis, unless you're a scientist.  Let's use a function to convert temperatures.\n",
    "\n",
    "---\n",
    "\n",
    "## You Do: Temperature Conversion\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "distant-income",
   "metadata": {},
   "source": [
    "Here are the formulas to use:\n",
    "\n",
    "\n",
    "* **Celsius to Kelvin** : `K = °C + 273`\n",
    "* **Fahrenheit to Kelvin** : `K = (5/9) * (°F - 32) + 273`\n",
    "\n",
    "Try to use one function to convert from either Fahrenheit or Celsius based upon a second parameter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "dirty-macintosh",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's pretty cold!\n"
     ]
    }
   ],
   "source": [
    "# create your function here\n",
    "def convert(temp, scale):\n",
    "    if scale == \"Fahrenheit\":\n",
    "        return (5/9) * (temp - 32) + 273\n",
    "    else:\n",
    "        return temp +  273\n",
    "\n",
    "# convert a temperature in either 'F' or 'C' into Kelvins using your function\n",
    "# store the result in a variable called 'temperature'\n",
    "# try out some different values and temperature scales\n",
    "temperature = convert(50,\"Fahrenheit\")\n",
    "\n",
    "\n",
    "if temperature > 299:\n",
    "    print(\"It's too hot!\")\n",
    "elif temperature <= 299 and temperature > 288:\n",
    "    print(\"It's just right!\")\n",
    "elif temperature <= 288 and temperature > 277:\n",
    "    print(\"It's pretty cold!\")\n",
    "else:\n",
    "    print(\"It's freezing!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "black-classic",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Exercise: Building a Copy\n",
    "\n",
    "Write a function, `copy_list`, that takes in a list, `original_list`, as a parameter. Your function should create a new list, `my_new_list` with the contents of the original list.  Your function should return `my_new_list`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "noble-punishment",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "def copy_list(original_list):\n",
    "    new_list = []\n",
    "    for item in original_list:\n",
    "        new_list.append(item)\n",
    "    return new_list\n",
    "#     alternative method using slicing\n",
    "#     return original_list[:]\n",
    "\n",
    "\n",
    "my_list = [1, 2, 3]\n",
    "my_new_list = copy_list(my_list)\n",
    "print(my_new_list)\n",
    "# Will print [1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cubic-metro",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Exercise: Reversing a List\n",
    "\n",
    "Write a function, `reverse_list`, that takes in a list, `my_list`, as a parameter. Your function should reverse the list and return it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "mechanical-limitation",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 2, 1]\n"
     ]
    }
   ],
   "source": [
    "# function defenition\n",
    "def reverse_list(my_list):\n",
    "    rev_list = []\n",
    "    for item in my_list:\n",
    "        rev_list.insert(0, item)\n",
    "    return rev_list\n",
    "#     alternate method using slicing\n",
    "#     return my_list[::-1]\n",
    "\n",
    "\n",
    "my_list = [1, 2, 3]\n",
    "reversed_list = reverse_list(my_list)\n",
    "print(reversed_list)\n",
    "# Will print [3, 2, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "right-terry",
   "metadata": {},
   "source": [
    "\n",
    "---\n",
    "\n",
    "## Summary + Q&A:\n",
    "\n",
    "Can you now:\n",
    "\n",
    "- Identify when to use a function?\n",
    "- Create and call a function with arguments?\n",
    "- Return a value from a function?\n"
   ]
  }
 ],
 "metadata": {
  "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.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
