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