{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Writing Functions\n", "=================\n", "\n", "We've already been using functions, but how do you write them on your own?\n", "\n", "In Python we do this using the following structure:\n", "\n", " def function_name(inputs):\n", " commands\n", " return output_value\n", "\n", "* `def` tells Python that we're creating a function\n", "* `function_name` is how we refer to the function in our program\n", "* the commands control what the function does with the `inputs`\n", "* `return` tells Python what output to send back to the rest of the program.\n", "\n", "Then indentation is important because it tells Python what is inside the function. You end a function by stopping the indenting.\n", "\n", "For example, if we wanted to write a function that converts pounds to kilograms:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "def convert_kg_to_g(weight_kg):\n", " weight_g = weight_kg * 1000\n", " return weight_g" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can then call that function exactly like we would one of Python's built in functions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "convert_kg_to_g(2.2)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "2200.0" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Functions can have multiple inputs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say we've fit a linear regression to some data and want to get a prediction based on the fitted parameters." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def predicted_value(a, b, x):\n", " prediction = a + b * x\n", " return prediction\n", "\n", "predicted_value(2, 1, 1)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "3" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define Functions Create Instructions\n", "------------------------------------\n", "\n", "* Defining a function creates a set of instructions, but doesn't do anything with them\n", "* Like describing to a friend how to make a PB&J sandwhich\n", "* When you call the function the computer follows those instructions and creates a value\n", "* If you want to do something with that value you need to store it in a variable\n", "* Functions need to be defined before they are called." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Local Variables\n", "---------------\n", "\n", "Variables created inside a Python function only exist inside that function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def convert_kg_to_g(weight_kg):\n", " weight_g = weight_kg * 1000\n", " return weight_g\n", "result = convert_kg_to_g(3.9)\n", "print weight_g" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'weight_g' is not defined", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mweight_g\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconvert_kg_to_g\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3.9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0mweight_g\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'weight_g' is not defined" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should also treat functions as if they don't know about variables outside of them, so they should only work with things that you pass them as input." ] } ], "metadata": {} } ] }