{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Writing Functions\n", "=================\n", "You're probably already familiar with [usingfunctions](http://nbviewer.ipython.org/urls/github.com/ethanwhite/progbio/raw/master/ipynbs/functions-using.ipynb), but how do you write you 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", "The `def` tells Python that we're creating a function, the `function_name` is how we refer to the function in our program, the commands control what the function does with the `inputs`, and the `return` statement tells Python what output to send back to the rest of the program. 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_lbs_to_kg(weight_lbs):\n", " weight_kg = weight_lbs / 2.20462\n", " return weight_kg" ], "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_lbs_to_kg(22)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "9.979044007584074" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define Functions Before Calling Them\n", "------------------------------------\n", "In the same way that we have to define a variable before we call it, we also have to define a function before we call it. So, the def statement must appear in the program before the function is used." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Local Variables\n", "---------------\n", "\n", "Variables created inside a Python function only exist inside that function. For example, if we define our weight conversion function, and then call it, the variable weight_kg does not exist after the function has been called." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def convert_lbs_to_kg(weight_lbs):\n", " weight_kg = weight_lbs / 2.20462\n", " return weight_kg\n", "result = convert_lbs_to_kg(170)\n", "print weight_kg" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'weight_kg' 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/home/ethan/Dropbox/Teaching/ProgBiol/repo/ipynbs/\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_kg\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_lbs_to_kg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m170\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_kg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'weight_kg' is not defined" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Software Carpentry video on functions\n", "" ] } ], "metadata": {} } ] }