{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "from __future__ import print_function" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# functions" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Functions are used to organize program flow, especially to allow us to easily do commonly needed tasks over and over again. We've already used a lot of functions, such as those that work on lists (`append()` and `pop()`) or strings (like `replace()`). Here we see how to write our own functions" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "A function takes arguments, listed in the `()` and returns a value. Even if you don't explictly give a return value, one will be return (e.g., `None`). \n", "\n", "Here's a simple example of a function that takes a single argument, `i`" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in the function, i = 10\n", "in the function, i = 5\n" ] } ], "source": [ "def my_fun(i):\n", " print(\"in the function, i = {}\".format(i))\n", " \n", "my_fun(10)\n", "my_fun(5)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in the function, i = 0\n", "None\n" ] } ], "source": [ "a = my_fun(0)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "functions are one place where _scope_ comes into play. A function has its own _namespace_. If a variable is not defined in that function, then it will look to the namespace from where it was called to see if that variable exists there. \n", "\n", "However, you should avoid this as much as possible (variables that persist across namespaces are called global variables).\n", "\n", "We already saw one instance of namespaces when we imported from the `math` module." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-----\n", "----------\n" ] } ], "source": [ "global_var = 10\n", "\n", "def print_fun(string, n):\n", " if n < global_var:\n", " print(string*n)\n", " else:\n", " print(string*global_var)\n", "\n", "print_fun(\"-\", 5)\n", "print_fun(\"-\", 20)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "global_var = 100" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "--------------------------------------------------\n" ] } ], "source": [ "print_fun(\"-\",50)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "By default, python will let you read from a global, but not update it." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in function outer = -100.0\n", "outside, outer = -100.0\n" ] } ], "source": [ "outer = 1.0\n", "\n", "def update():\n", " # uncomment this to allow us to access outer in the calling namespace\n", " global outer\n", " outer = -100.0\n", " print(\"in function outer = {}\".format(outer))\n", " \n", "update()\n", "print(\"outside, outer = {}\".format(outer))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "functions always return a value—if one is not explicitly given, then they return None, otherwise, they can return values (even multiple values) of any type" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "in the function, i = 10\n", "None\n" ] } ], "source": [ "a = my_fun(10)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "Here's a simple function that takes two numbers and returns their product." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12\n" ] } ], "source": [ "def multiply(a, b):\n", " return a*b\n", "\n", "c = multiply(3, 4)\n", "print(c)" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "