{"id":421,"date":"2023-02-15T11:30:09","date_gmt":"2023-02-15T11:30:09","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=421"},"modified":"2023-02-15T11:37:56","modified_gmt":"2023-02-15T11:37:56","slug":"variable-scope","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/variable-scope\/","title":{"rendered":"Variable scope"},"content":{"rendered":"\n<p>Variable scope refers to where in a program a variable can be accessed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Global scope<\/h2>\n\n\n\n<p>Variables made within the top level of the program, those outside of functions, are considered to have global scope. These can be accessed within any functions without having to be passed as a parameter.<\/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 testing_the_value_of_x():\n    print(\"The function still knows the value of x as\", x)\n\nx = 5\nprint(\"Outside the function the value of x is\", x)\ntesting_the_value_of_x()<\/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>Outside the function the value of x is 5\nThe function still knows the value of x as 5<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Local scope<\/h2>\n\n\n\n<p>Variables created within a function are considered to have local scope. They do not exist outside of the function in which they were created.<\/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 testing_the_value_of_x():\n    x = 10\n    print(\"Inside the function the value of x is\", x)\n\nx = 5\nprint(\"Outside the function the value of x is\", x)\ntesting_the_value_of_x()\nprint(\"Outside the function the value of x is still\", x)<\/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>Outside the function the value of x is 5\nInside the function the value of x is 10\nOutside the function the value of x is still 5<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>If you defined a function within another function then the scope would trickle inwards. <\/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 testing_the_value_of_x():\n    x = 10\n    print(\"Inside the function the value of x is\", x)\n    def inner_function():\n        y = 20\n        print(\"The inner function still knows the value of x as\", x)\n        print(\"The inner function value of y is\", y)\n    inner_function()\n    print(\"The outer function does not know the new value of y, it still has\", y)\n\nx = 5\ny = 1\nprint(\"Outside the function the value of x is\", x)\nprint(\"Outside the function the value of y is\", y)\ntesting_the_value_of_x()\nprint(\"Outside the function the value of x is still\", x)<\/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>Outside the function the value of x is 5\nOutside the function the value of y is 1\nInside the function the value of x is 10\nThe inner function still knows the value of x as 10\nThe inner function value of y is 20\nThe outer function does not know the new value of y, it still has 1\nOutside the function the value of x is still 5<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/challenge-7\/\" data-type=\"page\" data-id=\"428\">Next: Challenge 7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Variable scope refers to where in a program a variable can be accessed. Global scope Variables made within the top level of the program, those outside of functions, are considered to have global scope. These can be accessed within any functions without having to be passed as a parameter. Local scope Variables created within a&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-3\/variable-scope\/\">Continue reading <span class=\"screen-reader-text\">Variable scope<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":18,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-421","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/421","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=421"}],"version-history":[{"count":5,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/421\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/421\/revisions\/431"}],"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=421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}