{"metadata": {"name": "", "signature": "sha256:b4230c58d7d824ca8be03f74e46eb125c5df07b0d4f02fc37abf05fb782c9fbe"}, "nbformat_minor": 0, "nbformat": 3, "worksheets": [{"cells": [{"cell_type": "markdown", "source": ["Functions help extract out common code blocks.\n", "\n", "Let's define a function `print_greeting()`."], "metadata": {}}, {"input": ["def print_greeting():\n", " print(\"Hi there, how are you?\")\n", " print(\"Long time no see.\")"], "cell_type": "code", "outputs": [], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 1}, {"cell_type": "markdown", "source": ["And call it:"], "metadata": {}}, {"input": ["print_greeting()"], "cell_type": "code", "outputs": [{"text": ["Hi there, how are you?\n", "Long time no see.\n"], "stream": "stdout", "output_type": "stream"}], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 2}, {"cell_type": "markdown", "source": ["That's a bit impersonal."], "metadata": {}}, {"input": ["def print_greeting(name):\n", " print(\"Hi there, {0}, how are you?\".format(name))\n", " print(\"Long time no see.\")"], "cell_type": "code", "outputs": [], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 3}, {"input": ["print_greeting(\"Andreas\")"], "cell_type": "code", "outputs": [{"text": ["Hi there, Andreas, how are you?\n", "Long time no see.\n"], "stream": "stdout", "output_type": "stream"}], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 4}, {"cell_type": "markdown", "source": ["But we might not know their name.\n", "\n", "(And we just changed the interface of `print_greeting`!)"], "metadata": {}}, {"input": ["def print_greeting(name=\"my friend\"):\n", " print(\"Hi there, {0}, how are you?\".format(name))\n", " print(\"Long time no see.\")"], "cell_type": "code", "outputs": [], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 6}, {"input": ["print_greeting(\"Andreas\")\n", "print_greeting()"], "cell_type": "code", "outputs": [{"text": ["Hi there, Andreas, how are you?\n", "Long time no see.\n", "Hi there, my friend, how are you?\n", "Long time no see.\n"], "stream": "stdout", "output_type": "stream"}], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 7}, {"cell_type": "markdown", "source": ["----"], "metadata": {}}, {"cell_type": "markdown", "source": ["Function parameters work like variables.\n", "\n", "So what does this do?"], "metadata": {}}, {"input": ["def my_func(my_list):\n", " my_list.append(5)\n", " \n", "l = [1,2,3]\n", "my_func(l)\n", "print(l)"], "cell_type": "code", "outputs": [{"text": ["[1, 2, 3, 5]\n"], "stream": "stdout", "output_type": "stream"}], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 8}, {"cell_type": "markdown", "source": ["Can be very surprising!\n", "\n", "Define a better function `my_func_2`:"], "metadata": {}}, {"input": ["def my_func_2(my_list):\n", " return my_list + [5]"], "cell_type": "code", "outputs": [], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 9}, {"input": ["l = [1,2,3]\n", "l2 = my_func_2(l)\n", "print(l)\n", "print(l2)"], "cell_type": "code", "outputs": [{"text": ["[1, 2, 3]\n", "[1, 2, 3, 5]\n"], "stream": "stdout", "output_type": "stream"}], "language": "python", "metadata": {}, "collapsed": false, "prompt_number": 10}], "metadata": {}}]}