{ "cells": [ { "cell_type": "markdown", "id": "b232e068", "metadata": {}, "source": [ "# Functions\n", "\n", "Consider the expression `2 + 2`: whatever line of code it appears on, we evaluate it in its entirety before using it.\n", "\n", "For example, `x = 2 + 2` stores the _result of_ the `2 + 2` expression, rather than the expression itself. Thus, `x` now contains the value $4$.\n", "\n", "What if we wanted to do something more complicated than a simple arithmetic equation?" ] }, { "cell_type": "markdown", "id": "edc335d1", "metadata": {}, "source": [ "Let's take as an example a simple arithmetic equation: $2x + 1$. In Math, we might want to reference this equation elsewhere by saying that we should consider $f(x)$ to be that formula. Thus, everwhere we see $f(x)$, it's actually $2x + 1$. This allows us to plug in arbitrary values and build up results:\n", "\n", "$f(x) = 2x + 1\\\\\\text{let }x = 4\\\\f(4) = 9$\n", "\n", "We can do the same thing in programming, using \"**functions**\"." ] }, { "cell_type": "code", "execution_count": null, "id": "59999bc2", "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": null, "id": "dcac813f", "metadata": {}, "outputs": [], "source": [ "len(\"hello\")" ] }, { "cell_type": "code", "execution_count": null, "id": "750044b2", "metadata": {}, "outputs": [], "source": [ "len(\"\")" ] }, { "cell_type": "code", "execution_count": null, "id": "6cf3ea60", "metadata": {}, "outputs": [], "source": [ "math.sqrt(9)" ] }, { "cell_type": "code", "execution_count": null, "id": "fa140b9e", "metadata": {}, "outputs": [], "source": [ "math.sqrt(7)" ] }, { "cell_type": "code", "execution_count": null, "id": "73d968e4", "metadata": {}, "outputs": [], "source": [ "range(1, 5)" ] }, { "cell_type": "code", "execution_count": null, "id": "364f99eb", "metadata": {}, "outputs": [], "source": [ "range(8)" ] }, { "cell_type": "code", "execution_count": null, "id": "47b07952", "metadata": {}, "outputs": [], "source": [ "math.sin(0)" ] }, { "cell_type": "code", "execution_count": null, "id": "1f049b3c", "metadata": {}, "outputs": [], "source": [ "str(17)" ] }, { "cell_type": "markdown", "id": "aa497c36", "metadata": {}, "source": [ "Function call examples:" ] }, { "cell_type": "code", "execution_count": null, "id": "85a965a6", "metadata": {}, "outputs": [], "source": [ "x = 8\n", "y = 16\n", "z = math.sqrt(16)\n", "u = math.sqrt(y)\n", "v = math.sqrt(8 + 8)\n", "w = math.sqrt(x + x)\n" ] }, { "cell_type": "markdown", "id": "25b3a9ef", "metadata": {}, "source": [ "## Defining a new function\n", "\n", "Instead of $f(x) = 2x + 1$, we're going to **def**ine a new body of code, and give it the name `dbl_plus`:" ] }, { "cell_type": "code", "execution_count": null, "id": "aa14cf38", "metadata": {}, "outputs": [], "source": [ "def dbl_plus(x):\n", " return 2 * x + 1" ] }, { "cell_type": "markdown", "id": "d053afa0", "metadata": {}, "source": [ "We can then use (or \"call\") that code thusly:" ] }, { "cell_type": "code", "execution_count": null, "id": "16a78899", "metadata": {}, "outputs": [], "source": [ "dbl_plus(2)" ] }, { "cell_type": "code", "execution_count": null, "id": "5a11577a", "metadata": {}, "outputs": [], "source": [ "dbl_plus(3)" ] }, { "cell_type": "code", "execution_count": null, "id": "c3a1b34b", "metadata": {}, "outputs": [], "source": [ "x = 346\n", "dbl_plus(x)" ] }, { "cell_type": "markdown", "id": "cef5d0da", "metadata": {}, "source": [ "## Practice Reading" ] }, { "cell_type": "code", "execution_count": null, "id": "927cb53e", "metadata": {}, "outputs": [], "source": [ "x = 1\n", "z = 3.1\n", "\n", "def multiply(x, y):\n", " # (1) What are the values of x and y here\n", " # the first time multiply is called?\n", " print(\"x,y:\", x, y)\n", " z = 0\n", " return x * y\n", "\n", "x = 2\n", "y = 1\n", "result = multiply(3, y)\n", "print(y) # (2) What is the value of y here?\n", "result = multiply(x, 4)\n", "print(result) # (3) What is the value of result here?\n", "print(z) # (4) What is the value of z here?\n" ] }, { "cell_type": "code", "execution_count": null, "id": "e677f087", "metadata": {}, "outputs": [], "source": [ "def multiply(x, y):\n", " print(\"x,y:\", x, y)\n", " z = 0\n", " return x * y\n", "\n", "x = 2\n", "y = 1\n", "result = multiply(3, 4)\n", "print(result)" ] }, { "cell_type": "markdown", "id": "572e09d8", "metadata": {}, "source": [ "## More Definition Examples" ] }, { "cell_type": "code", "execution_count": null, "id": "33d8aed7", "metadata": {}, "outputs": [], "source": [ "def dbl_plus(x):\n", " return 2 * x + 1" ] }, { "cell_type": "code", "execution_count": null, "id": "4be95e39", "metadata": {}, "outputs": [], "source": [ "def instructor_name():\n", " return \"Alessia Fitz Gibbon\"" ] }, { "cell_type": "code", "execution_count": null, "id": "62575d10", "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " return x * x" ] }, { "cell_type": "code", "execution_count": null, "id": "d0acbd3a", "metadata": {}, "outputs": [], "source": [ "def calc_grade(points):\n", " grade = points * 10\n", " return grade" ] }, { "cell_type": "markdown", "id": "2d481695", "metadata": {}, "source": [ "## How many `x` variables? Which ones?" ] }, { "cell_type": "code", "execution_count": null, "id": "51f832f5", "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " return x * x\n", "\n", "def abs(x):\n", " if x < 0:\n", " return -x\n", " else:\n", " return x\n", "\n", "# main program\n", "x = 42\n", "sq3 = square(3)\n", "sq4 = square(4)\n", "print(sq3 + sq4)\n", "print(x)\n", "x = -22\n", "result = abs(x)\n", "print(result)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.2" } }, "nbformat": 4, "nbformat_minor": 5 }