{"id":707,"date":"2010-02-12T19:51:07","date_gmt":"2010-02-12T19:51:07","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=707"},"modified":"2010-02-12T19:51:07","modified_gmt":"2010-02-12T19:51:07","slug":"method-adding-two-numbers","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/12\/method-adding-two-numbers\/","title":{"rendered":"Method adding two numbers"},"content":{"rendered":"<p>Method programming with C++ is a way to save time when you need to redo same\/similar code, and also it allows for clearer code because when you read the code you can tell what is happening instead of just having the same code over and over again within the main method of the program.<\/p>\n<p>To declare a method within c++ there is syntax for the way that a method is defined.<\/p>\n<pre lang=\"cpp\">\r\n[return type] methodname(parameters if required...)\r\n<\/pre>\n<p>So for example if you want to pass in two integer variables and then to return a integer variable (the function could add the parameters together so shall we call it addTwoNumbers,<\/p>\n<pre lang=\"cpp\">\r\nint addTwoNumbers(int value1, int value2);\r\n<\/pre>\n<p>this means that the return value is a int (integer), you &#8220;call&#8221; the method by its name (addTwoNumbers) and pass in local variables to the method in the parameters (value1 and value2).<\/p>\n<p>Here is a full c++ code example that will read in two values from the keyboard and output the result to the screen.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <exception>\r\n#include <stdio.h>\r\n\r\n\/\/using the namespace std (standard) for the cin -> console input\r\nusing namespace std;       \r\n \r\nint addTwoNumbers(int val1, int val2)\r\n{\r\n    return (val1 + val2);\r\n}\r\n \r\nint main()\r\n{\r\n       \/\/ setup the defaul values.\r\n       int val1 =0, val2 =0;\r\n       try \r\n       {\r\n              \/\/ output to the console\r\n              printf(\"Please enter number 1 : \");\r\n              \/\/ read in the input, if not a integer value, this will\r\n              \/\/ cause a error \r\n              cin >> val1;       \r\n              printf(\"Please enter number 2 : \");\r\n              cin >> val2;\r\n       }\r\n       catch (exception& e)\r\n       {\r\n              \/\/ write out any error exception\r\n              printf(\"Not a valid input %s\\n\", e.what());\r\n       }\r\n       \/\/ output the answer of the two inputted values.\r\n       printf(\"Answer : %d\\n\", addTwoNumbers(val1, val2));\r\n       return 0;\r\n}<\/pre>\n<p>and here is the output <\/p>\n<pre lang=\"bash\">\r\nPlease enter number 1 : 20\r\nPlease enter number 2 : 10\r\nAnswer : 30\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Method programming with C++ is a way to save time when you need to redo same\/similar code, and also it allows for clearer code because when you read the code you can tell what is happening instead of just having the same code over and over again within the main method of the program. To &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/02\/12\/method-adding-two-numbers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Method adding two numbers<\/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":[],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/707","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=707"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":708,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/707\/revisions\/708"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}