{"id":265,"date":"2009-07-28T14:09:16","date_gmt":"2009-07-28T13:09:16","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=265"},"modified":"2009-07-28T14:09:16","modified_gmt":"2009-07-28T13:09:16","slug":"method-add-two-numbers-4","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/28\/method-add-two-numbers-4\/","title":{"rendered":"Method &#8211; Add Two numbers"},"content":{"rendered":"<p>This tutorial uses the same code as <a href=\"http:\/\/www.codingfriends.com\/index.php\/2009\/07\/28\/add-two-numbers-5\/\" title=\"Add two numbers\" target=\"_blank\">Add two numbers<\/a> but includes a function\/method to add two numbers together and return a value.  The addtwo function has two parameters that are returned back added together.  I have called them a and b, so that they are different names to the variables within the main method.<\/p>\n<p>The source code<\/p>\n<pre lang=\"ruby\">\r\n# define a function called addtwo with two parameters\r\ndef addtwo(a, b) \r\n       a + b; # return a + b\r\nend\r\n\r\nprint \"Please enter number 1 : \";\r\n# get the input from the console, \r\nval1 = gets;\r\nprint \"Please enter number 2 : \";\r\nval2 = gets;\r\n# convert the string console inputs to_i (to_integers) and add together\r\nprint \"Answer : \" , (val1.to_i + val2.to_i), \"\\n\";  \r\n<\/pre>\n<p>save as addtwonumbers_function.rb, this program will function the same as the previous tutorial apart from the inner working will call the method.  The method programming allows for one method to be called multiple times, e.g. Within the example above there could be a method that was 5 lines in length and instead of writing the same 5 lines each time, you just write a method that is called.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial uses the same code as Add two numbers but includes a function\/method to add two numbers together and return a value. The addtwo function has two parameters that are returned back added together. I have called them a and b, so that they are different names to the variables within the main method. &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/28\/method-add-two-numbers-4\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Method &#8211; Add 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":[20],"tags":[8,398,404,400],"class_list":["post-265","post","type-post","status-publish","format-standard","hentry","category-ruby","tag-gnu","tag-linux","tag-ruby","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/265","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=265"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/265\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}