{"id":412,"date":"2023-02-15T10:36:32","date_gmt":"2023-02-15T10:36:32","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=412"},"modified":"2023-04-26T08:45:05","modified_gmt":"2023-04-26T07:45:05","slug":"functions","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/functions\/","title":{"rendered":"Functions"},"content":{"rendered":"\n<p>A function allows a developer to create a set of statements to carry out a task that can then be called any number of times from within a program. This allows the program structure to be more readable by reducing it into logical blocks of code that each perform their own task. The top level of the code then just controls the flow between these function calls.<\/p>\n\n\n\n<p>In other languages you might hear functions referred to as subprograms, procedures, routines or subroutines.<\/p>\n\n\n\n<p>To create a function you use the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def function_name():\n    \/\/ indented section\n    \/\/ of code to run<\/code><\/pre>\n\n\n\n<p>The def keyword tells Python you want to define a function and then you give a name to the function that explains what it is doing.<\/p>\n\n\n\n<p>To call a function you then use the function name and parenthesis.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function_name()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Try this out now. Make a simple function to print a line of text and call it a couple of times.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def hello():\n    print(\"Hello world\")\n\nprint(\"Program starts\")\nhello()\nhello()\nprint(\"Program ends\")<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>Program starts\nHello world\nHello world\nProgram ends<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Passing values into functions<\/h2>\n\n\n\n<p>If you need to pass the function any arguments\/parameters they can be added into the parentheses as a comma separated collection:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def function_name(argument1, argument2):\n    print(\"The value of the first parameter is\", argument1)\n    print(\"The value of the second parameter is\", argument2)<\/code><\/pre>\n\n\n\n<p>You would then call the function as such:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function_name(\"Argument 1 is a string\", 12345)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<p>Let&#8217;s try it out:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def loop_text(text_to_show, times_to_repeat):\n    for counter in range(times_to_repeat):\n        print(text_to_show)\n\n# Top Level Program\n\nprint(\"Program starts\")\nloop_text(\"Hello world!\", 6)\nprint(\"Program ends\")<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>Program starts\nHello world!\nHello world!\nHello world!\nHello world!\nHello world!\nHello world!\nProgram ends<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Returning values from functions<\/h2>\n\n\n\n<p>The function can return values too using the return statement.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def add_two_numbers(number1, number2):\n    total = number1 + number2\n    return total\n\nprint(\"Program starts\")\nnumber1 = 10\nnumber2 = 5\nresult = add_two_numbers(number1, number2)\nprint(result)\nprint(\"Program ends\")<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>Program starts\n15\nProgram ends<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>If your function returns a value, you need to make sure that all paths through the function return a value. The following code would cause problems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_number_greater_than_10(number_to_check):\n    if number_to_check &gt; 10:\n        return \"Number is greater than 10\"\n    else:\n        print(\"Number is not greater than 10\")\n\nresult = check_number_greater_than_10(13)<\/code><\/pre>\n\n\n\n<p>If the number was not greater than 10 then a return line is never reached. The result would be a null or None value which could cause problems if your code was expecting a value. Here are two ways to avoid this.<\/p>\n\n\n\n<p>1 \u2013&nbsp;Always make sure there is a return line in each path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_number_greater_than_10(number_to_check):\n    if number_to_check &gt; 10:\n        return \"Number is greater than 10\"\n    else:\n        return \"Number is not greater than 10\"\n\nresult = check_number_greater_than_10(13)<\/code><\/pre>\n\n\n\n<p>2 \u2013&nbsp;Always return a value at the end of a function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def check_number_greater_than_10(number_to_check):\n    comment = \"Number is not greater than 10\"\n    if number_to_check &gt; 10:\n        comment = \"Number is greater than 10\"\n    \n    return comment\n\nresult = check_number_greater_than_10(13)<\/code><\/pre>\n\n\n\n<p>In the second option the function will always return a value at the end. You set a default value to be returned and then override that within the function. This way no matter what happens in your function code it will always end up at the return line.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Returning multiple values<\/h2>\n\n\n\n<p>In Python you can return multiple values in a return statement. You simply provide a comma separated list of variables to accept the returned values.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def add_two_numbers_and_average(number1, number2):\n    total = number1 + number2\n    average = total \/ 2\n    return total, average\n\nprint(\"Program starts\")\nnumber1 = 10\nnumber2 = 5\nresult, average = add_two_numbers_and_average(number1, number2)\nprint(result)\nprint(average)\nprint(\"Program ends\")<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>Program starts\n15\n7.5\nProgram ends<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_two_numbers_and_average(number1, number2):\n    total = number1 + number2\n    average = total \/ 2\n    return total, average\n\nnumber1 = 10\nnumber2 = 5\nresult, average = add_two_numbers_and_average(number1, number2)\nprint(result)\nprint(average)<\/code><\/pre>\n\n\n\n<p>The program does not understand what the variable is meant to do so it is the order in which the variables are returned that matches them into the outer variables. In the example above, the variable inside the function&nbsp;<strong>total&nbsp;<\/strong>is stored in the variable outside the function called&nbsp;<strong>result<\/strong>. This is because&nbsp;<strong>total<\/strong>&nbsp;is the first variable after the return keyword, and&nbsp;<strong>result<\/strong>&nbsp;is the first variable on the line where the function is called.<\/p>\n\n\n\n<p>In most programming languages if you wanted to send multiple bits of data back you\u2019d set up an array or class object to return because you wouldn&#8217;t be able to send multiple values in the return.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Procedures<\/h2>\n\n\n\n<p>A procedure is a function that does not return a value. Look at the examples below.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def print_area(length, breadth):\n    area = length * breadth\n    print(area)\n\n\nshape_length = 10\nshape_breadth = 3\nprint_area(shape_length, shape_breadth)<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>def calculate_area(length, breadth):\n    area = length * breadth\n    return area\n\nshape_length = 10\nshape_breadth = 3\narea = calculate_area(shape_length, shape_breadth)\nprint(area)<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>print_area is a procedure. Nothing is returned and it has no impact on the program\u2019s execution.<\/p>\n\n\n\n<p>calculate_area is a function. It returns a value which can be used within the program.<\/p>\n\n\n\n<hr class=\"wp-block-separator\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">Formal vs actual parameters<\/h2>\n\n\n\n<p>The formal parameter is how a variable is called within a function.<\/p>\n\n\n\n<p>The actual parameter is how a variable is called in the main program outside of the function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def print_name(fname, lname):\n    print(fname + \" \" + lname)\n\nfirst_name = \"John\"\nlast_name = \"Smith\"\n\nprint_name(first_name, last_name)<\/code><\/pre>\n\n\n\n<p>In this example the formal parameters are fname and lname.<\/p>\n\n\n\n<p>The actual parameters are first_name and last_name.<\/p>\n\n\n\n<p><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/variable-scope\/\" data-type=\"page\" data-id=\"421\">Next: Variable scope<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function allows a developer to create a set of statements to carry out a task that can then be called any number of times from within a program. This allows the program structure to be more readable by reducing it into logical blocks of code that each perform their own task. The top level&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/functions\/\">Continue reading <span class=\"screen-reader-text\">Functions<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":18,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-412","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/412\/revisions"}],"predecessor-version":[{"id":1348,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/412\/revisions\/1348"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/18"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}