{"id":37,"date":"2008-01-25T22:47:28","date_gmt":"2008-01-26T02:47:28","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=37"},"modified":"2008-01-25T22:50:27","modified_gmt":"2008-01-26T02:50:27","slug":"c-functions","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=37","title":{"rendered":"C++ Functions"},"content":{"rendered":"<p>Function<\/p>\n<ul>\n<li>Need to include function prototype before function can be used\n<ul>\n<li>void DoMagic(Magic name);<\/li>\n<\/ul>\n<\/li>\n<li>Passing parameters\n<ul>\n<li>C++ is pass by value<\/li>\n<li>Use pointer to pass by pointer (dah)<br \/>\n<code lang=\"cpp\"><br \/>\nvoid TestPassByPointer(){<br \/>\nint num = 10;<br \/>\nIncr10(&amp;num);<br \/>\nreturn;<br \/>\n}<br \/>\n\/\/ Pass by a copy of pointer<br \/>\n\/\/ so same memory address<br \/>\nint Incr10(int* num){<br \/>\n*num += 10;<br \/>\nreturn *num;<br \/>\n}<\/code><\/li>\n<li>Pass by reference<br \/>\n<code lang=\"cpp\"><br \/>\nvoid TestPassByRef(){<br \/>\nint num = 10;<br \/>\nint&amp; rnum = num;<br \/>\nIncr_10(rnum);<br \/>\nreturn;<br \/>\n}<br \/>\n\/\/ Pass by reference<br \/>\n\/\/ Changes the original variable directly<br \/>\nint Incr_10(int&amp; num){<br \/>\nnum += 10;<br \/>\nreturn num;<br \/>\n}<\/code><\/li>\n<li>Mandate that parameter not be changed\n<ul>\n<li>int increase10(const int&amp; num);<\/li>\n<\/ul>\n<\/li>\n<li>Initialize parameters\n<ul>\n<li>int myfxn(long arg1 = 10, long arg2 = 20);<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Return values\n<ul>\n<li>Return pointer\n<ul>\n<li>Never return the address of a local variable from a function (memory is gone!)<\/li>\n<li>Instead, return a pointer to free store and don&#8217;t forget to delete (memory leak!)<br \/>\n<code lang=\"cpp\"><br \/>\ndouble* treble(double data)<br \/>\n{<br \/>\n\/\/ Allocate memory in the heap<br \/>\ndouble* result = new double(0.0);<br \/>\n\/\/Check out of memory<br \/>\nif (!result){<br \/>\ncout &lt;&lt; \"Out of memory...\" &lt;&lt; endl;<br \/>\nexit(1);<br \/>\n}<br \/>\n*result = 3.0*data;<br \/>\nreturn result;<br \/>\n}<br \/>\nvoid TestReturnPointer()<br \/>\n{<br \/>\ndouble num = 10.0;<br \/>\ndouble* ptr = 0;<br \/>\nptr = treble(num);<br \/>\ncout &lt;&lt; \"three times \" &lt;&lt; num &lt;&lt; \" is \" &lt;&lt; *ptr &lt;&lt; endl;<br \/>\ndelete ptr; \/\/ Don't forget to delete!<br \/>\nreturn;<br \/>\n}<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Return reference<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Pointer to function<\/p>\n<ul>\n<li>Declare\n<ul>\n<li>return_type (*pointer_name) (list_of_parameter_types);<\/li>\n<li>double (*pfun) (char*, int);<\/li>\n<\/ul>\n<\/li>\n<li>Example<br \/>\n<code lang=\"cpp\"><br \/>\nvoid TestFunctionPointer()<br \/>\n{<br \/>\n\/\/ Declar a function pointer<br \/>\nlong (*fptr)(long, long);<br \/>\n\/\/ Assign fxn pointer to product<br \/>\nfptr = product;<br \/>\ncout &lt;&lt; fptr(3, 5) &lt;&lt; endl;<br \/>\n\/\/ Assign fxn pointer to sum<br \/>\nfptr = sum;<br \/>\ncout &lt;&lt; fptr(3, 5) &lt;&lt; endl;<br \/>\n}<br \/>\nlong product(long a, long b)<br \/>\n{<br \/>\nreturn a*b;<br \/>\n}<br \/>\nlong sum(long a, long b)<br \/>\n{<br \/>\nreturn a+b;<br \/>\n}<\/code><\/li>\n<li>Function pointer can be passed as parameter\n<ul>\n<li>double sumarray(double [] array, int len, double (*pfun)(double));<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Function Template<\/p>\n<ul>\n<li>Example<br \/>\n<code lang=\"cpp\"><br \/>\n\/\/ Define template<br \/>\ntemplate<class><\/class> T max(T x[], int len)<br \/>\n{<br \/>\nT max = x[0];<br \/>\nfor (int i = 0; i &lt; len; i++)<br \/>\n{<br \/>\nif (max &lt; x[i])<br \/>\nmax = x[i];<br \/>\n}<br \/>\nreturn max;<br \/>\n}<br \/>\nvoid TestFunctionTemplate()<br \/>\n{<br \/>\nint small[] = {1, 25, 35, 15};<br \/>\nint lensmall = sizeof small\/sizeof small[0];double medium[] = {25, 250, 2500, 5, 450};<br \/>\nint lenmedium = sizeof medium\/sizeof medium[0];cout &lt;&lt; \"Max of small: \" &lt;&lt; max(small, lensmall) &lt;&lt; endl;<br \/>\ncout &lt;&lt; \"Max of medium: \" &lt;&lt; max(medium, lenmedium) &lt;&lt; endl;<br \/>\n}<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Function Need to include function prototype before function can be used void DoMagic(Magic name); Passing parameters C++ is pass by value Use pointer to pass by pointer (dah) void TestPassByPointer(){ int num = 10; Incr10(&amp;num); return; } \/\/ Pass by &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=37\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-cpp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-B","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}