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