{"id":255,"date":"2023-06-13T14:33:35","date_gmt":"2023-06-13T14:33:35","guid":{"rendered":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?page_id=255"},"modified":"2023-06-13T14:43:27","modified_gmt":"2023-06-13T14:43:27","slug":"functions-procedures","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/","title":{"rendered":"Functions &#038; Procedures"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Functions<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Functions<\/h3>\n<p>In Python, functions are a way to group a set of statements together to perform a specific task. They allow you to organize your code into reusable blocks, making it easier to read, understand, and maintain. All functions must return a value using the <strong>return<\/strong> statement, otherwise it isn&#8217;t function &#8211; it&#8217;s a procedure.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Function definition<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">greet<\/span>():\r\n    return <span style=\"background-color: #fff0f0;\">\"Hello, world!\"<\/span>\r\n\r\n<span style=\"color: #888888;\"># Calling the function<\/span>\r\nprint(greet())  <span style=\"color: #888888;\"># Output: Hello, world!<\/span>\r\n<\/pre>\n<\/div>\n<p>Here the greet function returns the string &#8220;Hello, world!&#8221; to the program and the print function prints it out to the screen.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Input parameters<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Input Parameters<\/h3>\n<p>Functions can also take input values (arguments or parameters) and optionally return a result. Here&#8217;s an example of a function that takes arguments and returns a value:<\/p>\n<p>Here&#8217;s an example of defining and calling a simple function in Python:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Function definition with arguments and return value<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">add_numbers<\/span>(x, y):\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> x <span style=\"color: #333333;\">+<\/span> y\r\n\r\n<span style=\"color: #888888;\"># Calling the function and using the returned value<\/span>\r\nresult <span style=\"color: #333333;\">=<\/span> add_numbers(<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(result)  <span style=\"color: #888888;\"># Output: 8<\/span>\r\n<\/pre>\n<\/div>\n<p>In this example, the function <code>add_numbers<\/code> takes two arguments <code>x<\/code> and <code>y<\/code> and returns their sum. The returned value is stored in the <code>result<\/code> variable and printed.<\/p>\n<p><strong>Optional parameters &amp; default values<\/strong><\/p>\n<p>Python functions can have multiple parameters, and you can define default values for some or all of the parameters. Here&#8217;s an example:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Function definition with multiple parameters and default values<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">multiply_numbers<\/span>(x, y<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span>):\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> x <span style=\"color: #333333;\">*<\/span> y\r\n\r\n<span style=\"color: #888888;\"># Calling the function<\/span>\r\nresult <span style=\"color: #333333;\">=<\/span> multiply_numbers(<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(result)  <span style=\"color: #888888;\"># Output: 24<\/span>\r\n\r\nresult <span style=\"color: #333333;\">=<\/span> multiply_numbers(<span style=\"color: #0000dd; font-weight: bold;\">5<\/span>)\r\n<span style=\"color: #008800; font-weight: bold;\">print<\/span>(result)  <span style=\"color: #888888;\"># Output: 5 (default value of y is used)<\/span>\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n\n<\/div><h2 class=\"tabtitle\">Practice<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Resources<\/h3>\n<p><strong>Practice<\/strong><\/p>\n<p>You can practice writing your own functions using these <a href=\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\">Python functions challenges.<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>Exam Questions<\/p>\n<p><a href=\"https:\/\/pastpapers.papacambridge.com\/?dir=Cambridge%20International%20Examinations%20%28CIE%29\/AS%20and%20A%20Level\/Computer%20Science%20-%209608\">June 16 Paper 22 Questions 2,3<\/a><\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\"><\/h2>\n<div class=\"tabcontent responsive-tabs__panel--active\">\n\n","protected":false},"excerpt":{"rendered":"<p>Functions In Python, functions are a way to group a set of statements together to perform a specific task. They allow you to organize your code into reusable blocks, making it easier to read, understand, and maintain. All functions must return a value using the return statement, otherwise it isn&#8217;t function &#8211; it&#8217;s a procedure.&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Functions &#038; Procedures<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":100,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Functions &#038; Procedures - Edexcel iGCSE Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functions &#038; Procedures - Edexcel iGCSE Computer Science\" \/>\n<meta property=\"og:description\" content=\"Functions In Python, functions are a way to group a set of statements together to perform a specific task. They allow you to organize your code into reusable blocks, making it easier to read, understand, and maintain. All functions must return a value using the return statement, otherwise it isn&#8217;t function &#8211; it&#8217;s a procedure.&hellip;&nbsp;Read More &raquo;Functions &#038; Procedures\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\" \/>\n<meta property=\"og:site_name\" content=\"Edexcel iGCSE Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-13T14:43:27+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\",\"name\":\"Functions &#038; Procedures - Edexcel iGCSE Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\"},\"datePublished\":\"2023-06-13T14:33:35+00:00\",\"dateModified\":\"2023-06-13T14:43:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Functions &#038; Procedures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website\",\"url\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/\",\"name\":\"Edexcel iGCSE Computer Science\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functions &#038; Procedures - Edexcel iGCSE Computer Science","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":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/","og_locale":"en_GB","og_type":"article","og_title":"Functions &#038; Procedures - Edexcel iGCSE Computer Science","og_description":"Functions In Python, functions are a way to group a set of statements together to perform a specific task. They allow you to organize your code into reusable blocks, making it easier to read, understand, and maintain. All functions must return a value using the return statement, otherwise it isn&#8217;t function &#8211; it&#8217;s a procedure.&hellip;&nbsp;Read More &raquo;Functions &#038; Procedures","og_url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/","og_site_name":"Edexcel iGCSE Computer Science","article_modified_time":"2023-06-13T14:43:27+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/","name":"Functions &#038; Procedures - Edexcel iGCSE Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website"},"datePublished":"2023-06-13T14:33:35+00:00","dateModified":"2023-06-13T14:43:27+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/functions-procedures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/"},{"@type":"ListItem","position":2,"name":"Functions &#038; Procedures"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/#website","url":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/","name":"Edexcel iGCSE Computer Science","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Functions In Python, functions are a way to group a set of statements together to perform a specific task. They allow you to organize your code into reusable blocks, making it easier to read, understand, and maintain. All functions must return a value using the return statement, otherwise it isn&#8217;t function &#8211; it&#8217;s a procedure.&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/255"}],"collection":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/comments?post=255"}],"version-history":[{"count":3,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/255\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/pages\/255\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/edexcel-igcse-computer-science\/wp-json\/wp\/v2\/media?parent=255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}