{"id":1479,"date":"2024-04-26T07:17:10","date_gmt":"2024-04-26T07:17:10","guid":{"rendered":"https:\/\/learnlearn.uk\/python\/?page_id=1479"},"modified":"2024-04-26T07:19:44","modified_gmt":"2024-04-26T07:19:44","slug":"functions-and-procedures","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/","title":{"rendered":"Functions and Procedures"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Functions<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Introduction to Functions<\/h2>\n<h3 class=\"\">What are Functions?<\/h3>\n<p>Functions are blocks of code that perform a specific task and\u00a0<b>return a value<\/b>. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail).<\/p>\n<p>Functions are used when the result of a calculation or operation needs to be obtained<\/p>\n<h3 class=\"\">Function Example<\/h3>\n<pre class=\"\">def calculateSquare(num):\r\n\u00a0 \u00a0 return num * num\r\nresult = calculateSquare(5)<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Procedures<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Introduction to Procedures<\/h2>\n<h3 class=\"\">What are Procedures?<\/h3>\n<p>Procedures, also known as subroutines or methods, are blocks of code that perform a specific task\u00a0<b>without returning a value.<\/b>\u00a0They can accept input parameters and modify the values of variables. Procedures are typically used to group related code together and make the program more organized and modular.<\/p>\n<p>procedures are used when a specific task needs to be performed without a required result.<\/p>\n<h3 class=\"\">Procedure Example<\/h3>\n<pre class=\"\">def greetUser(name) :\r\n\u00a0 \u00a0 print(f\"Nice to meet you {name}\")\r\ngreetUser(\"John\"); \/\/ Output: Hello, John!\r\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Optional Params<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Optional Parameters<\/h2>\n<p class=\"\">In Python, a function parameter can have a<b>\u00a0default value assigned to it<\/b>, making it optional during the function call. This means that we can provide a default value for a parameter, which will be used if no value is explicitly passed when calling the function.<\/p>\n<p class=\"\">To define an optional parameter, we simply provide a default value to the parameter in the function definition. For example:<\/p>\n<pre>      \r\n        def greet(name, message=\"Hello\"):\r\n            print(message + \", \" + name)\r\n      \r\n<\/pre>\n<p>In the above code, the parameter &#8220;message&#8221; is optional with a default value of &#8220;Hello&#8221;. If no value is provided for &#8220;message&#8221; during the function call, it will default to &#8220;Hello&#8221;. However, if a value is provided, it will override the default value.<\/p>\n<p>We can now call the &#8220;greet&#8221; function with or without providing the &#8220;message&#8221; parameter:<\/p>\n<pre>      \r\n        greet(\"John\")  # Prints: Hello, John\r\n        greet(\"Jane\", \"Hi\")  # Prints: Hi, Jane\r\n      \r\n<\/pre>\n<p>Optional parameters provide flexibility in function design and make the code more readable. They allow us to define default behaviors for functions while still allowing customization when needed. This can save us from writing multiple versions of the same function with slight differences in behavior.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Required Params<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Required Parameters<\/h2>\n<p class=\"\">In Python, required parameters are the arguments that\u00a0<b>must be provided to a function<\/b>\u00a0when it is called. These parameters are mandatory and if not provided, the function will raise an error.<\/p>\n<p>Required parameters are defined within the parentheses of a function&#8217;s definition. When calling the function, the respective values for these parameters must be passed in the same order as they are defined.<\/p>\n<pre>      def greet(name, age):\r\n        print(f\"Hello {name}! You are {age} years old.\")\r\n\r\n      greet(\"Alice\", 16)\r\n<\/pre>\n<p>In the above example, the function greet requires two parameters: name and age. When calling the function, we pass the values for these parameters as &#8220;Alice&#8221; and 16 respectively, which will be used within the function&#8217;s body.<\/p>\n<p>If you forget to provide any of the required parameters when calling a function, Python will raise a TypeError with an appropriate error message indicating the missing arguments.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Params vs Args<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Parameters vs Arguments<\/h2>\n<p>Parameters are variables declared in a function&#8217;s signature to define the type and number of inputs the function can receive. They act as placeholders for data that will be passed to the function when it&#8217;s called.<\/p>\n<p>Arguments, on the other hand, are the specific values that are provided to the function when calling it. These values are assigned to the corresponding parameters within the function&#8217;s body, allowing the function to work with the given data. In essence, parameters establish the structure of the input the function expects, while arguments provide the actual data that fits that structure.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/6ed67236-e653-4fa0-8acb-193b77f54561.png\" \/><\/p>\n\n<\/div><h2 class=\"tabtitle\">Resources<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Resources<\/h3>\n<p><a href=\"https:\/\/revise.learnlearn.uk\/app\/dashboard\/11\/125\/274\/1035\">Online Revision<\/a><\/p>\n<p><a href=\"https:\/\/learnlearn.uk\/python\/python-functions-exercises\/\">Functions Challenges<\/a><\/p>\n<p><a href=\"https:\/\/docs.google.com\/document\/d\/1yUBb2KaJpUlUT-g4szBQcyzHjVBGDkFbkEgPU0dkrgc\/edit?usp=sharing\">Validation Functions Exercises<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Functions What are Functions? Functions are blocks of code that perform a specific task and\u00a0return a value. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail). Functions are used when the result&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Functions and 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 and Procedures - Python<\/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\/python\/functions-and-procedures\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functions and Procedures - Python\" \/>\n<meta property=\"og:description\" content=\"Introduction to Functions What are Functions? Functions are blocks of code that perform a specific task and\u00a0return a value. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail). Functions are used when the result&hellip;&nbsp;Read More &raquo;Functions and Procedures\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-26T07:19:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/6ed67236-e653-4fa0-8acb-193b77f54561.png\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/\",\"name\":\"Functions and Procedures - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2024-04-26T07:17:10+00:00\",\"dateModified\":\"2024-04-26T07:19:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Functions and Procedures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functions and Procedures - Python","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\/python\/functions-and-procedures\/","og_locale":"en_GB","og_type":"article","og_title":"Functions and Procedures - Python","og_description":"Introduction to Functions What are Functions? Functions are blocks of code that perform a specific task and\u00a0return a value. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail). Functions are used when the result&hellip;&nbsp;Read More &raquo;Functions and Procedures","og_url":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/","og_site_name":"Python","article_modified_time":"2024-04-26T07:19:44+00:00","og_image":[{"url":"https:\/\/revise.learnlearn.uk\/django-summernote\/2023-08-27\/6ed67236-e653-4fa0-8acb-193b77f54561.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/","url":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/","name":"Functions and Procedures - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2024-04-26T07:17:10+00:00","dateModified":"2024-04-26T07:19:44+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/functions-and-procedures\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/functions-and-procedures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Functions and Procedures"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction to Functions What are Functions? Functions are blocks of code that perform a specific task and\u00a0return a value. They are reusable and can be called multiple times within a program. Functions are designed to accomplish a specific objective and are usually named with a verb (e.g., calculateSum, validateEmail). Functions are used when the result&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1479"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":3,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1479\/revisions"}],"predecessor-version":[{"id":1483,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/1479\/revisions\/1483"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}