{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions\n", "\n", "\n", "\n", "You've already seen some of Python's built-in functions:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(1, 3)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "16" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pow(2, 4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You've also seen functions in math. For example:\n", "\n", "\$f(x) = 2x + 7\$\n", "\n", "This is the **definition** of \$f(x)\$. By itself it doesn't have a value. It just defines what it would mean to\n", "call the function \$f\$ on some value.\n", "\n", "Given a **value** for \$x\$, we can calculate the resulting value of \$f(x)\$.\n", "\$f(2)\$ evaluates to 11, \$f(-1\$) gives 5.\n", "\n", "In Python, in addition to using the built-in functions, we can define our own. For example, we can define a function equivalent to \$f(x) = 2x + 7\$:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def f(x):\n", " return 2 * x + 7" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(-1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the general form of a function definition:\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`def`: \n", "- a keyword indicating a function definition\n", "\n", "`<>`: \n", "- the name of the function (use `pothole_case` name format)\n", "\n", "`<>`: \n", "- the parameter(s) of the function, 0 or more. \n", "- Parameters are a comma-separated list of variables whose values will be supplied when the function is called.\n", "\n", "`<>`: \n", "- One or more statements, often ending with a `return` statement. \n", "- All body statements must be indented the same amount (usually 4 spaces)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `return` statement has this form:\n", " `return «expression»`\n", " \n", "The `return` statement is executed as follows:\n", "- Evaluate the expresssion. The produces a value (which has a memory address).\n", "- Exit the function and produce that value to the caller.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once a function has been defined, we can call on the function. A *function call* has the form:\n", " `«function_name»(«arguments»)`\n", "\n", "A function call is executed as follows:\n", "- Evaluate the arguments. These produce values. Each value has a memory address.\n", "- Assign those values to the parameters. This stores the memory addresses of the values in the parameters.\n", "- Pause the current statement and execute the body of the function\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Practice Exercise: Functions and Terminology\n", "\n", "Revisiting our earlier example, the function definition was:\n", "\n", "
```\n",
"def f(x):\n",
"    return 2 * x + 7\n",
"```
\n", "\n", "Answer the following questions:\n", "1. What is the name of the function?\n", "2. How many parameter(s) does the function have? \n", "3. What are the parameter(s) names?\n", "4. For function call `f(8)`, what is the argument?\n", "5. When it is executed, what value does `f(8)` produce?\n", "6. Consider this code: `result = f(4)`. After that statement is executed, what value does `result` refer to?\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Function Design Recipe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When defining functions, we will follow this **Function Design Recipe**:\n", "\n", "1. Write an **example** call (or two) of how we will use the function we plan to write.\n", "2. Write the **header** for the function. That's the `def` line.\n", "3. Write the **type** contract -- the type for each parameter and the type of value that will be returned.\n", "4. Write a **description** of what the function does.\n", "5. Finally, write the **code** for the body of the function.\n", "6. Then **test** our function.\n", "\n", "Let's write a function that squares a number.\n", "\n", "Step 1: Give examples of how we will call the function:\n", "
```\n",
">>> square(5)\n",
"25\n",
">>> square(-6)\n",
"36\n",
"```