{"id":210,"date":"2017-07-20T14:15:11","date_gmt":"2017-07-20T13:15:11","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=210"},"modified":"2017-07-20T21:51:57","modified_gmt":"2017-07-20T20:51:57","slug":"python-functions","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=210","title":{"rendered":"Python: Functions"},"content":{"rendered":"<p>A function allows frequently used sections of code to be grouped together into a self contained unit making code resuse and maintenance much easier. These functions are then called upon as often as needed from other parts of your program. To define a function we start with the <strong>def<\/strong> statement followed by the name we want to call the function. When defining a function we can also supply values called a parameters to be passed to the function. Any code belonging to the function needs to be indented and should be applied in multiples of four spaces ending with a blank line:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef energy(mass):\r\n    # Function returns the energy (joules) for a given mass (E=MC2)\r\n    speed = 299792458  # Speed of light (metres per second)\r\n    return long((mass * speed)**2)\r\n\r\ndef show_results(mass):\r\n    # Subroutine displays the mass, speed and energy\r\n    print &quot;&quot;\r\n    print &quot;Mass    (kg):  %s&quot; % (mass)\r\n    print &quot;Speed  (mps):  299792458&quot;\r\n    print &quot;---------------------------------&quot;\r\n    print &quot;Energy   (J):  %s&quot; % (energy(mass))\r\n\r\n# Displays mass, speed and energy\r\nmass = 1.56\r\nshow_results(mass)\r\n<\/pre>\n<p>In the above example the computer works its way down the code and when it reaches the <strong>def<\/strong> statements it does nothing but make a note of it. It isn&#8217;t until reaching line 15 that it assigns the value <strong>1.56<\/strong> to the variable called <strong>mass<\/strong>. Line 16 then takes the value of mass and calls the <strong>show_results<\/strong> function passing the value 1.56. The show_results function in turn calls the <strong>energy<\/strong> function which performs the calculation and returns the results; the show_results fuction then presents these results.<\/p>\n<p>Output:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nMass:    (kg):  1.56\r\nSpeed:  (mps):  299792458\r\n----------------------------------\r\nEnergy    (J):  218721060297391968\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function allows frequently used sections of code to be grouped together into a self contained unit making code resuse and maintenance much easier. These functions are then called upon as often as needed from other parts of your program. To define a function we start with the def statement followed by the name we [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":73,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Functions - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=210\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Functions - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"A function allows frequently used sections of code to be grouped together into a self contained unit making code resuse and maintenance much easier. These functions are then called upon as often as needed from other parts of your program. To define a function we start with the def statement followed by the name we [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=210\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-20T20:51:57+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=210\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=210\",\"name\":\"Python: Functions - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-20T13:15:11+00:00\",\"dateModified\":\"2017-07-20T20:51:57+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=210#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=210\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=210#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=73\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python: Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Functions - Phantom Raspberry Blower","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=210","og_locale":"en_GB","og_type":"article","og_title":"Python: Functions - Phantom Raspberry Blower","og_description":"A function allows frequently used sections of code to be grouped together into a self contained unit making code resuse and maintenance much easier. These functions are then called upon as often as needed from other parts of your program. To define a function we start with the def statement followed by the name we [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=210","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2017-07-20T20:51:57+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=210","url":"http:\/\/www.bulis.co.uk\/?page_id=210","name":"Python: Functions - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-20T13:15:11+00:00","dateModified":"2017-07-20T20:51:57+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=210#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=210"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=210#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Python Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=73"},{"@type":"ListItem","position":4,"name":"Python: Functions"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/210"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/210\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/73"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}