{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# B - Functions\n", "\n", "![lec-0B-ga](lec-0B-ga.png)\n", "\n", "* To try it on *python tutor live*, [click here][preloaded ptl B]\n", "\n", "[preloaded ptl B]: http://www.pythontutor.com/live.html#code=%23%20%3D%3D%3D%20Lecture%20zero,%20part%20B%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%20---%20Remember%20from%20part%20A%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Amain_R%20%3D%208.314%20%23%20we%20will%20define%20R%20as%20we%20did%20before,%20however%20we%20will%20name%20it%20main_R%20so%20we%20will%20have%20clear%20distinctions%20between%20the%20different%20scopes%20of%20our%20code%0A%23%20---%20macro-like%20functions%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20macro_V(%29%3A%20%23%20we%20can%20functions%20depending%20on%20current%20values%20of%20local%20variables%20main_T,%20main_P,%20main_R%0A%09return%20main_R%20*%20main_T%20/%20main_P%0Amain_T%20%3D%20273%20%23%20and%20use%20it%20like%20this%3A%0Amain_P%20%3D%201e5%0Aprint(macro_V(%29%29%20%23note%20the%20empty%20parentheses%20that%20indicates%20the%20macro-like%20function%20should%20be%20evaluated%20based%20on%20current%20values%20of%20any%20variables%20it%20depends%20on%20-%20in%20this%20case,%20main_R,%20main_T%20and%20main_P%20-.%0Amain_T%20%3D%20298%20%23%20and%20again%0Amain_P%20%3D%201e5%0Aprint(macro_V(%29%29%0Amain_T%20%3D%20373%20%23%20and%20again%0Amain_P%20%3D%201e5%0Aprint(macro_V(%29%29%0A%23%20---%20functions%20of%20dummy%20arguments%20-------------------------------------------------------------------------------------------------------------------------------------------------------------------------%0Adef%20function_V(dummy_T,dummy_P%29%3A%20%23%20and%20we%20can%20do%20functions%20of%20arbitrary%20dummy_T%20and%20dummy_P,%20and%20also%20depending%20on%20the%20current%20value%20of%20main_R%0A%09return%20main_R%20*%20dummy_T%20/%20dummy_P%0Aprint(function_V(dummy_T%3D273,dummy_P%3D1e5%29%29%20%23%20now%20we%20can%20use%20this%20function%20like%20this%0Aprint(function_V(dummy_T%3D298,dummy_P%3D1e5%29%29%20%23%20where%20the%20actual%20arguments%20-%20teh%20values%20273%20and%201e5%20-%20take%20the%20places%20of%20the%20dummy%20arguments%20-%20dummy_T%20and%20dummy_P,%20respectively%20-.%0Aprint(function_V(dummy_T%3D373,dummy_P%3D1e5%29%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-live.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.02269722\n", "0.02477572\n", "0.03101122\n", "0.02269722\n", "0.02477572\n", "0.03101122\n" ] } ], "source": [ "# The following code should be studied on the platform provided by http://www.pythontutor.com/live.html#mode=edit\n", "# This platform graphically depicts the behavior of variables, assignments, functions and pointers,\n", "# which is the major source of confusion among beginners in programming\n", "\n", "# === Lecture zero, part B ========================================================================================================================================================================\n", "\n", "# --- Remember from part A -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# we will define R as we did before, however we will name it main_R so we will have clear distinctions between the different scopes of our code\n", "\n", "# instead of\n", "#R = 8.314\n", "# we will do\n", "main_R = 8.314\n", "\n", "# but instead of\n", "\n", "#V = R * T / P # \"*\" means multiplication, \"/\" means division\n", "\n", "# --- macro-like functions -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\n", "# we can functions depending on current values of local variables main_T, main_P, main_R\n", "\n", "def macro_V():\n", "\treturn main_R * main_T / main_P\n", "\n", "# and use it like this:\t\n", "\n", "main_T = 273\n", "main_P = 1e5\n", "print(macro_V()) #note the empty parentheses that indicates the macro-like function should be evaluated based on current values of any variables it depends on - in this case, main_R, main_T and main_P -.\n", "\n", "main_T = 298\n", "main_P = 1e5\n", "print(macro_V())\n", "\n", "main_T = 373\n", "main_P = 1e5\n", "print(macro_V())\n", "\n", "# --- functions of dummy arguments -------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n", "\t\n", "# and we can do functions of arbitrary dummy_T and dummy_P, and also depending on the current value of main_R\n", "def function_V(dummy_T,dummy_P):\n", "\treturn main_R * dummy_T / dummy_P\n", "\n", "# now we can use this function as follows:\n", "\n", "print(function_V(dummy_T=273,dummy_P=1e5)) # where the actual arguments - teh values 273 and 1e5 - take the places of the dummy arguments - dummy_T and dummy_P, respectively -.\n", "print(function_V(dummy_T=298,dummy_P=1e5))\n", "print(function_V(dummy_T=373,dummy_P=1e5))\n", "\n", "# functionizing the calculation of volume comes in hand when we want to make recursive usage of it -- see Lecture zero, part C" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }