{"id":220,"date":"2022-05-24T10:18:32","date_gmt":"2022-05-24T09:18:32","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/?page_id=220"},"modified":"2024-11-13T10:54:32","modified_gmt":"2024-11-13T10:54:32","slug":"parameter-passing","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/parameter-passing\/","title":{"rendered":"Parameter Passing"},"content":{"rendered":"<h2 style=\"color: red\">Parameters<\/h2>\n<p>A\u00a0 parameter is a variable or value that is passed into and\/or out of a subprogram.\u00a0 There are two types of parameters.<\/p>\n<ul>\n<li>The <strong>formal<\/strong> parameters are a placeholder for the actual parameter<\/li>\n<li>The <strong>actual<\/strong> parameters are the values passed into the subprogram<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-221\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24101822\/Screenshot-2022-05-24-101803-300x152.png\" alt=\"\" width=\"853\" height=\"432\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24101822\/Screenshot-2022-05-24-101803-300x152.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24101822\/Screenshot-2022-05-24-101803.png 607w\" sizes=\"auto, (max-width: 853px) 100vw, 853px\" \/><\/p>\n<ul>\n<li><strong><span style=\"color: #ff0000\">Formal parameters contains the placeholder of the actual parameters .<\/span><\/strong><\/li>\n<li><strong><span style=\"color: #ff0000\">Any changes to the formal parameters are automatically made to the actual parameter.<\/span><\/strong><\/li>\n<li><strong><span style=\"color: #ff0000\">The Actual parameters contain the value<\/span><\/strong><\/li>\n<\/ul>\n<h2 style=\"color: red\">Passing Parameters by Reference\u00a0 and by Value<\/h2>\n<h3>Parameter Passing by R<strong>eference<\/strong><\/h3>\n<p>Parameter passing by <strong>reference<\/strong> is used\u00a0 when the value being passed in is to be <strong>updated and passes back out again.<\/strong> (The variable itself is passed into the subprogram so that any changes to the variable will change the variable.<\/p>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$1\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$1.0\">If a programmer passes a parameter by reference, then the subprogram or function has\u00a0<\/span><strong data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$1.$1\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$1.$1.0\">direct access to the memory location<\/span><\/strong><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$1.2\">\u00a0holding the value of the variable.<\/span><\/p>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$2\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$2.$2.0\">This means that any change to the value during the execution of the subprogram would apply when the variable was next used in the main program.<\/span><\/p>\n<h3>Parameter Passing by Value<\/h3>\n<p>Parameter passing by <b>value <\/b>is used\u00a0 when a value is passed into a subprogram b<strong>ut does not require to be passed out<\/strong>. (The subprogram makes a copy of the variable so that any changes to the copy of the variable will not change the variable.<\/p>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$1\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$1.0\">If a programmer passes a parameter by value, then a\u00a0<\/span><strong data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$1.$1\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$1.$1.0\">temporary second copy of the value of the variable is made and held in RAM<\/span><\/strong><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$1.2\">.<\/span><\/p>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$2\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$2.0\">This copy can be changed within a subprogram but the change would not affect the original value held in the main program\u2019s local variable.<\/span><\/p>\n<h3 data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$3\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$3.0\">Drawback<\/span><\/h3>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$4\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$4.0\">The main drawback is the increased demand on RAM.\u00a0 <\/span><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$5.0\">As well as having to store the original value of the local variable, it is also necessary to store a second copy in main memory as well.<\/span><\/p>\n<h3 data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$6\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$6.0\">Advantage<\/span><\/h3>\n<p data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$7\"><span data-reactid=\".89no0tn8ye.0.0.0.1:0.1.0.$0.$3.$7.0\">The main advantage is that the value of the original local variable is protected from accidental or unnecessary change.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Parameters A\u00a0 parameter is a variable or value that is passed into and\/or out of a subprogram.\u00a0 There are two types of parameters. The formal parameters are a placeholder for the actual parameter The actual parameters are the values passed into the subprogram Formal parameters contains the placeholder of the actual parameters . Any changes [&hellip;]<\/p>\n","protected":false},"author":79984,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-220","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/220","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/users\/79984"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/comments?post=220"}],"version-history":[{"count":9,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/220\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/220\/revisions\/352"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/media?parent=220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}