{"id":89,"date":"2008-08-23T18:24:49","date_gmt":"2008-08-24T02:24:49","guid":{"rendered":"http:\/\/hongyu.org\/wordpress\/?p=89"},"modified":"2014-01-31T01:04:46","modified_gmt":"2014-01-31T09:04:46","slug":"c-java-and-c-comparison-function-and-parameters","status":"publish","type":"post","link":"https:\/\/www.hongyu.org\/wordpress\/?p=89","title":{"rendered":"Comparison of passing function parameters: C++, Java, C#, Perl, PHP, JavaScript and Visual Basic"},"content":{"rendered":"<p>Parameters can be transferred between the main program and its functions\/methods using either value parameters (by value) or reference parameters (by reference). With all the different languages that I used before, this topic comes out from time to time. So I think it would better for me to summarize them at one place:<\/p>\n<p><strong>C++ <\/strong><br \/> parameters can be passed either by value or by reference<\/p>\n<ul>\n<li>pass parameters by value<br \/> example: myfunction(myclass c)<\/li>\n<li>pass by reference<br \/> example: myfunction(myclass &amp;c)<\/li>\n<\/ul>\n<p><strong>Java <\/strong><br \/> pass parameters by value <strong>only<\/strong>.<br \/> One of the consequences of such constraint is that there is no easy way to implement the simple swap(x,y) function as in C++ or C#<\/p>\n<p>see the <a href=\"http:\/\/javadude.com\/articles\/passbyvalue.htm\">comment here<\/a><\/p>\n<p><strong>C#<\/strong><br \/> similar to C++, parameters can be passed either <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/0f66670z(VS.71).aspx\" target=\"_blank\">by value or by reference<\/a><\/p>\n<ul>\n<li>pass parameters by value<br \/> example: myfunction(int i)<\/li>\n<li>pass by reference<br \/> example: myfunction(<strong>ref<\/strong> int i) or myfunction(<strong>out <\/strong>int i).<br \/> Difference: <strong>ref<\/strong> needs the variable initialized before being called, while <strong>out<\/strong> doesn&#8217;t, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/szasx730(VS.71).aspx\" target=\"_blank\">the microsoft array example<\/a><\/li>\n<\/ul>\n<p>One uniqueness of C# compared to Java and C++ is its variable type, which will affect its behavior in passing parameters:<\/p>\n<ul>\n<li>similar to Java but different from C++, C# class is a reference type, so the variables of class object are accessed via their references<\/li>\n<li>similar to C++ but absent in Java, C# struct is a value type, so struct instances are accessed directly, not by references<\/li>\n<\/ul>\n<p><strong>Perl<\/strong><br \/> Pass by reference. The original variables in the main program are passed to subroutine in the special array @_. See <a href=\"http:\/\/www.cs.cf.ac.uk\/Dave\/PERL\/node51.html\">comment here<\/a>.<\/p>\n<p><strong>PHP 5 <\/strong><br \/> Similar to C++, parameters can be passed by value or reference.<\/p>\n<p>example: myfunction(myclass &amp;$c)<\/p>\n<p><a href=\"http:\/\/usphp.com\/manual\/en\/language.references.pass.php\">See the manual here<\/a>.<\/p>\n<p><strong>JavaScript<\/strong><br \/> similar to Java, parameters are passed by value only. <a href=\"http:\/\/blog.opensourceopportunities.com\/2008\/07\/javascript-and-java-are-pass-by-value.html\">See comment here<\/a>.<\/p>\n<p><strong>Visual Basic <\/strong><br \/> Similar to C++, parameters can be passed by value or reference.<\/p>\n<p>example: Sub Myfunction1(ByVal v As Integer), Sub Myfunction2(ByRef v As Integer)<\/p>\n<p><strong>Oracle PL\/SQL\u00c2\u00a0<\/strong><br \/> By default OUT and IN OUT parameters are passed by value and IN parameters are passed by reference, but NOCOPY keyword can be used to change the OUT and IN OUT parameters to be passed by reference. See <a href=\"http:\/\/www.dba-oracle.com\/t_pl_sql_nocopy_data_structures.htm\">reference here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Parameters can be transferred between the main program and its functions\/methods using either value parameters (by value) or reference parameters (by reference). With all the different languages that I used before, this topic comes out from time to time. So I think it would better for me to summarize them at one place: C++ parameters &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.hongyu.org\/wordpress\/?p=89\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Comparison of passing function parameters: C++, Java, C#, Perl, PHP, JavaScript and Visual Basic&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/89"}],"collection":[{"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":3,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"predecessor-version":[{"id":458,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions\/458"}],"wp:attachment":[{"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongyu.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}