{"id":693,"date":"2024-08-09T09:44:29","date_gmt":"2024-08-09T04:44:29","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=693"},"modified":"2024-10-05T09:05:05","modified_gmt":"2024-10-05T04:05:05","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-functions\/","title":{"rendered":"Python: Functions"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-fe9cc265 wp-block-group-is-layout-flex\">\n<p>Functions are a way to achieve modularity and reusability in code. Before moving forward we need to know what are these:<\/p>\n\n\n\n<p><strong>Modularity<\/strong>: Modular programming is the process of subdividing a computer program into separate sub-programs. A module can often be used in various applications and functions with other components of the system.<\/p>\n\n\n\n<p><strong>Reusability:<\/strong> Using already developed code according to our requirements without writing from scratch.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p>In Python, we define a function with a keyword <strong><em>def<\/em><\/strong>  then the function name after the name of the function we supply a pair of parentheses and a colon sign <strong>():<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add():\nnumber1 = int(input('Enter a value'))\nnumber2 = int(input('Enter another value'))\nprint(number1 + number2)<\/strong><\/code><\/pre>\n\n\n\n<p>Note that every statement which is part of the function body is a level indented more than the definition of the function.<\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We have declared a function named <strong>add<\/strong> now we can call it as many times and in any module as we want. To call the function we just need to write the name of function followed by <strong>pair of parentheses<\/strong>.<\/p>\n\n\n\n<p class=\"has-large-font-size\"><strong>add()<\/strong><\/p>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Passing information positional arguments<\/h3>\n\n\n\n<p>A generic function does not define any data it processes inside it hard-coded instead it accepts the data when it is called and processes that data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add(number1, number2):\n       print (number1 + number2)<\/strong><\/code><\/pre>\n\n\n\n<p>Now to call the function we need to pass two arguments and they are matched according to their position in the function call. For example: here, value 3 will be assigned to number1 while value 5 will be assigned to number2 variable.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>add(3, 5)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Passing information keyword arguments<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add(number1, number2):\n       print (number1 + number2)<\/strong><\/code><\/pre>\n\n\n\n<p>There is another way to call the same function that we pass the arguments with the variable name, this way position does not matter but the value is assigned to the matching name variable in function parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>add(number2 = 5, number1 = 3)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Default value parameters<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add(number1 = 0, number2 = 0):<\/strong>\n     <strong>print(number1 + number2)<\/strong><\/code><\/pre>\n\n\n\n<p>There are times when some parameter values are optional but still, you need a default value in case someone does not provide the value to avoid any non-deterministic behaviours.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>add(number2 = 5)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\">Dealing with an unknown number of arguments<\/h3>\n\n\n\n<p>In some cases, we can not guess how many arguments the user would pass when calling the function so we need a parameter that can take all values provided by the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def display_nums(first_num, second_num,opt_nums):\n      print(first_num)\n      print(second_num)\n      print(opt_nums)<\/strong><\/code><\/pre>\n\n\n\n<p>Here we see a parameter with <strong>(*)<\/strong>, this parameter will deal with an <strong>arbitrary number<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h4 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:800\">Passing information back from them<\/h4>\n\n\n\n<p>Functions not only perform a given task when they are called. But a function may also <strong>return<\/strong> some value to the user. This returned value can be assigned, reused and modified then. For this, we use the <strong><em>return<\/em><\/strong> keyword <\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:800\">Using functions as variables<\/h3>\n\n\n\n<p>Functions can be used as variables. This is done by using function calls in our expressions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add (a,b):\n     return a+b\n\ndef sub(b,a):\n     return a-b\n\nresult = add(2, 3) + sub(2,3)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:800\">Local vs. global variables<\/h3>\n\n\n\n<p><strong>Local Variables<\/strong> are the variables defined inside the functions. Their scope is only inside the function. They are not accessible outside the function.<\/p>\n\n\n\n<p><strong>Global variables<\/strong> are the variables defined outside the function and can be accessed and modified in and outside the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>def add (a,b):\n     return a+b\n\ndef sub(b,a):\n     return a-b\n\nresult = add(2, 3) + sub(2,3)<\/strong><\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading\" style=\"font-style:normal;font-weight:800\">Functions within functions<\/h3>\n\n\n\n<p>Right now we have learned how to call function. We can call a function inside another function providing the required signature of the function. This technique is called <strong>Nested Functions<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>commissionCalc(sales):\n   if sales>100:\n       return sales*100\n   else:\n       return 0<\/strong>\n\n<strong>\ndef salaryCalc (basic) :\n     grossSalary = basic + commissionCalc()\n     print (f\" your gross salary is {grossSalary}\")<\/strong><\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Functions are a way to achieve modularity and reusability in code. Before moving forward we need to know what are these: Modularity: Modular programming is the process of subdividing a computer program into separate sub-programs. A module can often be used in various applications and functions with other components of the system. Reusability: Using already [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"5","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-693","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":28,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":782,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/693\/revisions\/782"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}