{"id":102,"date":"2009-07-23T21:46:07","date_gmt":"2009-07-23T20:46:07","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=102"},"modified":"2009-07-23T21:56:44","modified_gmt":"2009-07-23T20:56:44","slug":"local-variables","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/23\/local-variables\/","title":{"rendered":"Local variables"},"content":{"rendered":"<p><span id=\"zipfile\"><a href='http:\/\/www.codingfriends.com\/wp-content\/uploads\/2009\/07\/localvariables.zip'><\/a><\/span>This is a tutorial about variable scope, a variable is a means to store data within the context of the program. For example, if you wish to store a numerical value then you would not store that number in a string, but within a integer or floating point number. <\/p>\n<p>There are different types of variables, integer \/ string etc, but the scope of a variable is the area in which the variable is defined within block of code that it is situated.<\/p>\n<p>This is some c++ code to demonstrate the difference between local variables and global.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n\r\n\/\/ global variable\r\nint value1 = 0;\r\n\r\nvoid globalVariable()\r\n{\r\n       \/\/ output the global variable\r\n       std::cout << \"Global \" << value1 << \"\\n\";\r\n}\r\n\r\nint main(void)\r\n{\r\n       int value1 = 1;                     \/\/ local function variable\r\n       globalVariable();              \/\/ output the global\r\n       std::cout << \"Local \" << value1 << \"\\n\";       \/\/ output the local\r\n       \r\n       \/\/ the value1 here is local to the for loop\r\n       for (int value1 = 10; value1 < 15; value1++)\r\n       {\r\n              std::cout << \"For loop : \" << value1 << \"\\n\";\r\n       }\r\n       \r\n       \/\/ output the global and local and notice they have not changed with the very local for loop \r\n       \/\/ with the same variable name.\r\n       globalVariable();\r\n       std::cout << \"Local \" << value1 << \"\\n\";\r\n       return 0;       \r\n}\r\n<\/pre>\n<p>the output would be<\/p>\n<pre class=\"consoleoutput\">\r\nGlobal 0\r\nLocal 1\r\nFor loop : 10\r\nFor loop : 11\r\nFor loop : 12\r\nFor loop : 13\r\nFor loop : 14\r\nGlobal 0\r\nLocal 1\r\n<\/pre>\n<p>To demonstrate that the global \/local and looping locals do only work within there area.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a tutorial about variable scope, a variable is a means to store data within the context of the program. For example, if you wish to store a numerical value then you would not store that number in a string, but within a integer or floating point number. There are different types of variables, &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/23\/local-variables\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Local variables<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,9,8,398,400],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":7,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}