{"id":2712,"date":"2020-09-28T08:50:38","date_gmt":"2020-09-28T08:50:38","guid":{"rendered":"https:\/\/idiagress.com\/vinaaypatil\/?p=2712"},"modified":"2023-10-28T12:50:00","modified_gmt":"2023-10-28T12:50:00","slug":"1-7-user-defined-functions-in-python","status":"publish","type":"post","link":"https:\/\/idiagress.com\/vinaaypatil\/2020\/09\/28\/1-7-user-defined-functions-in-python\/","title":{"rendered":"1.7 User Defined Functions in Python"},"content":{"rendered":"\n<p>In our previous example of finding the roots of a quadratic equation using the Bisection Method, we observed that a piece of code was repeated twice: once to calculate the positive root and then again to find the negative root. This repetition involves the same logic with just a change in variables.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Bisection loop for positive roots<\/i><br><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) != _c <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) &gt; _c:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_high = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_low = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_iter = _iter+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;At&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iteration solution is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;After &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iterations the positive root of Equation is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>For the negative roots, we repeat the same code with a change in variables: <\/p>\n\n\n\n<p class=\"has-text-align-center\">&#8216;<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">_nhigh<\/mark><\/strong>&#8216; replaces &#8216;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">_high<\/mark>,&#8217; <\/p>\n\n\n\n<p class=\"has-text-align-center\">and <\/p>\n\n\n\n<p class=\"has-text-align-center\">&#8216;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">_nlow<\/mark>&#8216; replaces &#8216;<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">_low<\/mark>.&#8217;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Bisection loop for negative roots<\/i><br><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) != _c <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) &gt; _c:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_nhigh = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_nlow = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_iter = _iter+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_nhigh + _nlow)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;At&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iteration solution is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;After &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iterations the negative root of Equation is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x) _x)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>When a piece of code repeats with only variable values changing, Python offers a powerful feature: User-Defined Functions (UDFs). By defining a UDF at the start of our program, we can call the function anytime within the program to repeat the code. Let&#8217;s illustrate this with the same Bisection Loop code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Defining a User-Defined Function<\/h3>\n\n\n\n<p>To define a UDF in Python, you use the <code>def<\/code> command. You assign a name to the function and declare the variables to be passed into it.<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>def bisection_loop(_x, _high, _low, _iter=0):<\/code><\/pre>\n\n\n\n<p>In this function definition, &#8216;_x,&#8217; &#8216;_high,&#8217; &#8216;_low,&#8217; and &#8216;_iter&#8217; are the variables that change values during execution. &#8216;_iter&#8217; is assigned a default value of &#8216;0.&#8217; <\/p>\n\n\n\n<p><em>We can define any \u2018names\u2019 for the variables. We can use names like \u2018a,\u2019 \u2018b\u2019 also. However, to avoid confusion at this early stage, we are sticking to previous variable names. Additionally, we can set the default value for a variable in the function definition itself. We have done this for \u2018<b>_iter<\/b>\u2019 and set the default value to \u2018<b>0\u2019<\/b><\/em>.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n\n\n\n<p>We declare the rest of our code below the \u2018def\u2019 command.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #859905; font-family: Courier; font-size: medium;\">def<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low, _iter=<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) != _c <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) &gt; _c:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_high = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_low = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_iter = _iter+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;At&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iteration solution is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;After &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iterations the positive root of Equation is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Once declared at the start of the program, we can call the function later in the program using the following syntax &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function_name(variable1, variable2, ..., variableN)<\/code><\/pre>\n\n\n\n<p>In the case of our function, we can call it in the following manner &#8211;<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bisection_loop (2, 50, 5 )<\/code><\/pre>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Here&#8217;s how Python interprets the call:<\/p>\n\n\n\n<p><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low, _iter=<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">): <span class=\"Apple-converted-space\">&nbsp; <\/span>corresponds to <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(2,50,5)<\/span> <\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>_x<\/code> corresponds to <code>2<\/code><\/li>\n\n\n\n<li><code>_high<\/code> corresponds to <code>50<\/code><\/li>\n\n\n\n<li><code>_low<\/code> corresponds to <code>5<\/code><\/li>\n\n\n\n<li><code>_iter<\/code> gets the default value of <code>0<\/code><\/li>\n<\/ul>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Once we understand this correspondence, we can use the same function for calculating the positive and negative roots by merely manipulating the values passed into the function.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br>_x = (_nhigh + _nlow)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _nhigh, _nlow)<\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We need to change the value of \u2018_x\u2019 before passing it into the function. For the other two values, we already have two different variables in our program. The full program will be as follows &#8211; (bisection_function.py)<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span style=\"color: #000000; font-family: Avenir; font-size: medium;\"><b>bisection_function.py<\/b><\/span><\/td><\/tr><tr><td><span style=\"color: #859905; font-family: Courier; font-size: medium;\">def<\/span> <span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low, _iter=<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) != _c <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) &gt; _c:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_high = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_low = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_iter = _iter+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;At&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iteration solution is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;After &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _iter, <\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8221; iterations the positive root of Equation is: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">, _x)<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><span class=\"Apple-converted-space\">&nbsp;&nbsp; &nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># program to find roots of the equation<\/i><br><i># format of equation<span class=\"Apple-converted-space\">&nbsp; <\/span>Ax2 + Cx = C<\/i><br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">print<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter coefficients in the format Ax2 + Bx = C&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br>_a = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the coefficient of x2<span class=\"Apple-converted-space\">&nbsp; <\/span>: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br>_b = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the coefficient of x <span class=\"Apple-converted-space\">&nbsp; <\/span>: &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br>_c = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the coefficient on RHS : &#8220;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Define interval<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_low = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the lower interval for solution<span class=\"Apple-converted-space\">&nbsp; <\/span>:&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br>_high = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">float<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the higher interval for solution :&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Negative interval<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_nlow = -(_low)<br>_nhigh = -(_high)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><i># Define iteration _limit<\/i><br><\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_limit = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">int<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">input<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(<\/span><span style=\"color: #2aa198; font-family: Courier; font-size: medium;\">&#8220;Enter the iteration limit for solution :&#8221;<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">))<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low)<br><span class=\"Apple-converted-space\">&nbsp; <\/span><br>_x = (_nhigh + _nlow)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _nhigh, _nlow)<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We get a much crisper code by using the function, now let us see if it works<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"636\" height=\"599\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python.png\" alt=\"\" class=\"wp-image-2713\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python.png 636w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python-300x283.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python-335x316.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/Output_functions_bisection_python-600x565.png 600w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>We can make a few improvements to the code :<\/p>\n\n\n\n<p>1. Firstly, since we are passing values, no need to have separate variables \u2018<b>_nhigh<\/b>\u2019 and \u2018<b>_nlow<\/b>\u2019. We can pass negative values of \u2018_high\u2019 and \u2018_low directly into the function<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, _high, _low)<\/span><\/p>\n\n\n\n<p><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_x, -(_high), -(_low))<\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>2. Secondly, we can insert the calculation for \u2018_x\u2019 within the function.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>Implementing our improvements, we get a condensed code.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions.png\"><img loading=\"lazy\" decoding=\"async\" width=\"733\" height=\"677\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions.png\" alt=\"\" class=\"wp-image-2714\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions.png 733w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions-300x277.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions-335x309.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/improved_python_program_bisection_loop_using_functions-600x554.png 600w\" sizes=\"auto, (max-width: 733px) 100vw, 733px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output for our improved program is as follows<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"605\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1.png\" alt=\"\" class=\"wp-image-2715\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1.png 615w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1-300x295.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1-70x70.png 70w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1-335x330.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_function_python_1-600x590.png 600w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The output is the same as the previous programs.<span class=\"Apple-converted-space\">&nbsp;<\/span><\/p>\n\n\n\n<p>Upon running our program with the user-defined function, you might notice an unexpected output. Specifically, the last line of the print statement for the negative root, &#8216;\u201327.97&#8217;, mentions, &#8220;After 10 iterations, the <strong>positive<\/strong> root of Equation is: \u201329.97.&#8221; Why does this happen?<\/p>\n\n\n\n<p>The issue arises because the variable &#8216;_x&#8217; is declared within our function, and the &#8216;print&#8217; command falls outside the function scope. To resolve this and improve the clarity of our code, we can include an &#8216;if&#8230;else&#8217; branching to check for the sign of the root.<\/p>\n\n\n\n<p>However, this approach can make our code look somewhat clumsy. Moreover, another problem remains: we are not storing the roots. In each execution of the function, &#8216;_x&#8217; is overwritten, preventing us from retaining both the positive and negative roots for further use.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><b>&nbsp;Return option in Python Functions<\/b><\/h2>\n\n\n\n<p>To resolve both issues\u2014correcting the output discrepancy and storing the roots\u2014Python functions provide a handy feature known as &#8216;return.&#8217; This feature allows us to obtain a value from the function. In our case, we aim to return the value of &#8216;_x.&#8217;<\/p>\n\n\n\n<p>Here&#8217;s how we can incorporate the &#8216;return&#8217; statement at the end of our &#8216;bisection_loop&#8217; function:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>&nbsp;<\/td><\/tr><tr><td><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">(_high, _low, _iter=<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">0<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">):<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">while<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) != _c <\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">and<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"> _iter &lt; _limit:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">if<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"> ((_a*_x*_x) + (_b*_x)) &gt; _c:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">_high = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">else<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">:<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">_low = _x<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">_iter = _iter+<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">1<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; &nbsp; &nbsp; <\/span><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">_x = (_high + _low)\/<\/span><span style=\"color: #d33682; font-family: Courier; font-size: medium;\">2<br><\/span><span style=\"color: #dcd0a9; font-family: Courier; font-size: medium;\"><span class=\"Apple-converted-space\">&nbsp; &nbsp; <\/span><\/span><span style=\"color: #859905; font-family: Courier; font-size: medium;\">return<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"> _x<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"color: #000000; font-family: Iowan Old Style; font-size: small;\">We can then store the returned value into another variable as follows &#8211;<\/span><\/h3>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">_proot = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(_high, _low)<br>_nroot = <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #657b83; font-family: Courier; font-size: medium;\">(-(_high), -(_low)) <\/span><span style=\"color: #657b83; font-family: Helvetica; font-size: medium;\"><br><span class=\"Apple-converted-space\">&nbsp;&nbsp;<\/span><\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">\u2018<\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>_proot<\/b><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\"><b>\u2019<\/b> will store the \u2018<\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>returned<\/b><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">\u2019 value of \u2018<\/span><span style=\"color: #000000; font-family: Courier; font-size: medium;\"><b>_x<\/b><\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">\u2019 from <\/span><span style=\"color: #268bd2; font-family: Courier; font-size: medium;\">bisection_loop<\/span><span style=\"color: #93a1a1; font-family: Courier; font-size: medium;\">(_high, _low)<\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p><span style=\"color: #000000; font-family: Iowan Old Style; font-size: small;\">Similarly we will store the value in \u2018_nroot\u2019 (<i> Important : We can return only one value from the function)<\/i><\/span><\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>It&#8217;s crucial to note that a function can return only one value. This usage of the &#8216;return&#8217; feature enhances the clarity and functionality of our code, allowing us to easily manage and utilize the roots. Moreover, the triple double-inverted commands within the function definition act as comments, making the code more readable and self-explanatory<\/p>\n\n\n\n<p>The Bisection loop program with \u2018return\u2019 statement is as follows<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"719\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python.png\" alt=\"\" class=\"wp-image-2716\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python.png 720w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-150x150.png 150w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-300x300.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-70x70.png 70w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-335x335.png 335w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-360x360.png 360w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-600x599.png 600w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/bisection_loop_function_with_return_python-100x100.png 100w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>The triple double-inverted commands within the function definition act as comments. These comments make the code more \u2018readable\u2019.<\/p>\n\n\n\n<p>The output is as follows<\/p>\n\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/return_function_python.png\"><img loading=\"lazy\" decoding=\"async\" width=\"580\" height=\"203\" src=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/return_function_python.png\" alt=\"\" class=\"wp-image-2717\" srcset=\"https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/return_function_python.png 580w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/return_function_python-300x105.png 300w, https:\/\/idiagress.com\/vinaaypatil\/wp-content\/uploads\/2020\/09\/return_function_python-335x117.png 335w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/a><\/figure>\n<\/div>\n\n\n<div style=\"height:46px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n<div class=\"vw-infobox\"><div class=\"vw-infobox-inner\"><h4 class=\"vw-infobox-title\"><span>Exercise<\/span><\/h4><div class=\"vw-infobox-content\"><\/p>\n<ol>\n<li style=\"text-align: left;\">Implement a program with a function definition. The function compares two numbers and returns the larger number of the two.<span class=\"Apple-converted-space\">\u00a0<\/span><\/li>\n<li style=\"text-align: left;\">Implement a program with a function definition. Three numbers enter the function and the function determines whether the numbers are part of a pythagorean triad.<span class=\"Apple-converted-space\">\u00a0<\/span>(e.g. 3, 4 and 5 are a triad because 5<sup>2<\/sup> = 4<sup>2<\/sup> + 3<sup>2<\/sup> , similarly 20, 21, 29)<\/li>\n<\/ol>\n<ol start=\"3\">\n<li style=\"text-align: left;\">Implement a program with a function definition. Five numbers enter the function, and the average of the numbers is returned.<\/li>\n<li style=\"text-align: left;\">Implement a program with a function definition. The input is height of the student in feet and inches ( 5ft 10inch). The function converts and returns the height in cm.<\/li>\n<li style=\"text-align: left;\">In the above program, implement an additional function definition. Heights of three students enter the function, the function calculates and returns the average height in cm. Another function then converts the average height back to feet and inches.<\/li>\n<\/ol>\n<p><\/div><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\" start=\"4\">\n<li><strong>Roots of Quadratic Equation:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Applied bisection to find the roots of a quadratic equation, providing an example using Python.<\/li>\n\n\n\n<li>Explored the challenges of determining the solution interval and dealing with both positive and negative roots.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Creating User Defined Functions:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Introduced User Defined Functions (UDFs) to avoid repetitive code.<\/li>\n\n\n\n<li>Demonstrated how to define a UDF using the &#8216;def&#8217; statement and return values from it.<\/li>\n\n\n\n<li>Utilized UDFs to simplify the code for finding the roots of the quadratic equation.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Improving Code with &#8216;Return&#8217; Statements:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Explained the usage of the &#8216;return&#8217; statement to retrieve values from a function.<\/li>\n\n\n\n<li>Showcased how &#8216;return&#8217; can enhance code readability and store results for further use.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><a class='vw-button vw-button--purple ' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/09\/04\/1-6-loops-and-iterations\/' target='_self'>Prev Chapter<\/a> &nbsp; <a class='vw-button vw-button-- ' href='https:\/\/idiagress.com\/vinaaypatil\/first-steps-in-python\/' target='_self'>Course Homepage<\/a> &nbsp; &nbsp;<a class='vw-button vw-button--pink  vw-button--arrow' href='https:\/\/idiagress.com\/vinaaypatil\/2020\/11\/21\/1-8-comparing-lists-using-functions\/' target='_self'>Next Chapter<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous example of finding the roots of a quadratic equation using the Bisection Method, we observed that a piece of code was repeated twice: once to calculate the positive root and then again to find the negative root. This repetition involves the same logic with just a change in variables. &nbsp;# Bisection loop &#8230;<\/p>\n","protected":false},"author":2,"featured_media":2768,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[412],"tags":[426,417,416,418,394],"class_list":["post-2712","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-first-steps-in-python","tag-bisection-method","tag-branching-in-python","tag-functions-in-python","tag-loops-in-python","tag-python"],"_links":{"self":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2712","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/comments?post=2712"}],"version-history":[{"count":4,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2712\/revisions"}],"predecessor-version":[{"id":3829,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/posts\/2712\/revisions\/3829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media\/2768"}],"wp:attachment":[{"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/media?parent=2712"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/categories?post=2712"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idiagress.com\/vinaaypatil\/wp-json\/wp\/v2\/tags?post=2712"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}