{"id":2975,"date":"2024-05-21T07:13:20","date_gmt":"2024-05-21T07:13:20","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=2975"},"modified":"2024-05-21T07:13:22","modified_gmt":"2024-05-21T07:13:22","slug":"variable-scope","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/05\/21\/variable-scope\/","title":{"rendered":"Variable Scope"},"content":{"rendered":"\n<p>A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable cannot be accessed. There are three places, where variables can be declared in Pascal programming language \u2212<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Inside a subprogram or a block which is called local variables<\/li>\n\n\n\n<li>Outside of all subprograms which is called global variables<\/li>\n\n\n\n<li>In the definition of subprogram parameters which is called formal parameters<\/li>\n<\/ul>\n\n\n\n<p>Let us explain what are&nbsp;<strong>local<\/strong>&nbsp;and&nbsp;<strong>global<\/strong>&nbsp;variables and formal parameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Local Variables<\/h2>\n\n\n\n<p>Variables that are declared inside a subprogram or block are called local variables. They can be used only by statements that are inside that subprogram or block of code. Local variables are not known to subprograms outside their own. Following is the example using local variables. Here, all the variables&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c<\/em>&nbsp;are local to program named&nbsp;<em>exLocal<\/em>.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/Mo7dVR\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program exLocal; \nvar\n   a, b, c: integer;\n\nbegin\n   (* actual initialization *)\n   a := 10;\n   b := 20;\n   c := a + b;\n   \n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\nend.<\/code><\/pre>\n\n\n\n<p>When the above code is compiled and executed, it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>value of a = 10 b = 20 c = 30\n<\/code><\/pre>\n\n\n\n<p>Now, let us extend the program little more, let us create a procedure named display, which will have its own set of variables&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c<\/em>&nbsp;and display their values, right from the program&nbsp;<em>exLocal<\/em>.<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/1VRD6E\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program exLocal;\nvar\n   a, b, c: integer;\nprocedure display;\n\nvar\n   a, b, c: integer;\nbegin\n   (* local variables *)\n   a := 10;\n   b := 20;\n   c := a + b;\n   \n   writeln('Winthin the procedure display');\n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\nend;\n\nbegin\n   a:= 100;\n   b:= 200;\n   c:= a + b;\n   \n   writeln('Winthin the program exlocal');\n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\n   display();\nend.<\/code><\/pre>\n\n\n\n<p>When the above code is compiled and executed, it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Within the program exlocal\nvalue of a = 100 b = 200 c = 300\nWithin the procedure display\nvalue of a = 10 b = 20 c = 30\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Global Variables<\/h2>\n\n\n\n<p>Global variables are defined outside of a function, usually on top of the program. The global variables will hold their value throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.<\/p>\n\n\n\n<p>A&nbsp;<strong>global<\/strong>&nbsp;variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. Following is an example using&nbsp;<strong>global<\/strong>&nbsp;and&nbsp;<strong>local<\/strong>&nbsp;variables \u2212<\/p>\n\n\n\n<p><a href=\"http:\/\/tpcg.io\/b1BUKF\" target=\"_blank\" rel=\"noreferrer noopener\">Live Demo<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program exGlobal;\nvar\n   a, b, c: integer;\nprocedure display;\nvar\n   x, y, z: integer;\n\nbegin\n   (* local variables *)\n   x := 10;\n   y := 20;\n   z := x + y;\n   \n   (*global variables *)\n   a := 30;\n   b:= 40;\n   c:= a + b;\n   \n   writeln('Winthin the procedure display');\n   writeln(' Displaying the global variables a, b, and c');\n   \n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\n   writeln('Displaying the local variables x, y, and z');\n   \n   writeln('value of x = ', x , ' y =  ',  y, ' and z = ', z);\nend;\n\nbegin\n   a:= 100;\n   b:= 200;\n   c:= 300;\n   \n   writeln('Winthin the program exlocal');\n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\n   \n   display();\nend.<\/code><\/pre>\n\n\n\n<p>When the above code is compiled and executed, it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Within the program exlocal\nvalue of a = 100 b = 200 c = 300\nWithin the procedure display\nDisplaying the global variables a, b, and c\nvalue of a = 30 b = 40 c = 70\nDisplaying the local variables x, y, and z\nvalue of x = 10 y = 20 z = 30\n<\/code><\/pre>\n\n\n\n<p>Please note that the procedure display has access to the variables a, b and c, which are global variables with respect to display as well as its own local variables. A program can have same name for local and global variables but value of local variable inside a function will take preference.<\/p>\n\n\n\n<p>Let us change the previous example a little, now the local variables for the procedure display has same names as&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>,&nbsp;<em>c<\/em>&nbsp;\u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>program exGlobal;\nvar\n   a, b, c: integer;\nprocedure display;\n\nvar\n   a, b, c: integer;\n\nbegin\n   (* local variables *)\n   a := 10;\n   b := 20;\n   c := a + b;\n   \n   writeln('Winthin the procedure display');\n   writeln(' Displaying the global variables a, b, and c');\n   \n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\n   writeln('Displaying the local variables a, b, and c');\n   \n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);\nend;\n\nbegin\n   a:= 100;\n   b:= 200;\n   c:= 300;\n   \n   writeln('Winthin the program exlocal');\n   writeln('value of a = ', a , ' b =  ',  b, ' and c = ', c);   \n   \n   display();\nend.<\/code><\/pre>\n\n\n\n<p>When the above code is compiled and executed, it produces the following result \u2212<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Within the program exlocal\nvalue of a = 100 b = 200 c = 300\nWithin the procedure display\nDisplaying the global variables a, b, and c\nvalue of a = 10 b = 20 c = 30\nDisplaying the local variables a, b, and c\nvalue of a = 10 b = 20 c = 30\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable cannot be accessed. There are three places, where variables can be declared in Pascal programming language \u2212 Let us explain what are&nbsp;local&nbsp;and&nbsp;global&nbsp;variables and formal parameters. Local Variables Variables that are declared inside [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[226],"tags":[],"class_list":["post-2975","post","type-post","status-publish","format-standard","hentry","category-06-pascal"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2975","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=2975"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2975\/revisions"}],"predecessor-version":[{"id":2976,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/2975\/revisions\/2976"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=2975"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=2975"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=2975"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}