{"id":6458,"date":"2015-04-06T11:11:31","date_gmt":"2015-04-06T03:11:31","guid":{"rendered":"http:\/\/www.vbtutor.net\/?page_id=6458"},"modified":"2018-06-22T18:04:54","modified_gmt":"2018-06-22T10:04:54","slug":"visual-basic-2015-lesson-16-understanding-sub-procedures","status":"publish","type":"page","link":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/","title":{"rendered":"Visual Basic 2015 Lesson 16: Understanding Sub Procedures"},"content":{"rendered":"<h4 style=\"text-align: center;\"><a title=\"visual basic tutorial lesson 15\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-15-looping-repetitive-jobs\/\">[Lesson 15]<\/a> &lt;&lt; <a title=\"visual basic 2015 tutorial \" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt; <a title=\"visual basic 2015 tutorial lesson 17\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-17-functions\/\">[Lesson 17]<\/a><\/h4>\n<p>A procedure is a program code that can carry out certain tasks or return a value. It can be called from other procedures. In Visual Basic 2015, there are two types of procedures; \u00a0sub procedures and functions. A sub procedure(also call subroutine) is a procedure that performs a specific task and does\u00a0not return\u00a0a value while a function is a procedure that returns a value. We will learn about function in next lesson.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nA sub procedure is usually used to accept input from the user, display information, print information, manipulate properties or perform some other tasks. It is a program code by itself and it is not an event procedure because it is not associated with a runtime procedure. It is called by other code\u00a0whenever it is required to perform a certain task.<\/p>\n<p>Sub procedures help to make programs smaller and easier to manage. A sub procedure begins with a Sub keyword and ends with an End Sub keyword. The program structure of a sub procedure is as follows:<\/p>\n<pre style=\"font-size: 110%;\">Sub ProcedureName (parameter)\r\nStatements\r\n End Sub<\/pre>\n<p>The parameter is a certain data that is passed into the sub procedure to perform a specified task.<\/p>\n<h3>Example 16.1<\/h3>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">In this example, we create a sub procedure, to sum up two values that are specified as the parameters. The main program can reference a procedure by using its name together with the parameters in the parentheses. <\/span><\/p>\n<pre style=\"font-size: 110%;\">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\nsum(5, 6)\r\nEnd Sub \r\n\r\nSub sum(a As Single, b As Single) \r\nMsgBox(\"sum=\"&amp; a + b)\r\nEnd Sub \r\n<\/pre>\n<p><span style=\"font-size: 1rem; line-height: 1.714285714;\">Running the program produces a message box<\/span><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4444\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\" alt=\"vb2013_figure11.1\" width=\"154\" height=\"155\" \/><\/a><\/p>\n<p align=\"center\"><strong>Figure 16.1<\/strong><\/p>\n<h4>Example 16.2: Password Cracker<\/h4>\n<p>This is Visual Basic 2015 program that demonstrates how to crack passwords. It can generate possible passwords and compare each of them with the actual password; and if the generated password found to be equal to the actual password, login will be successful.<\/p>\n<p>In this program, a timer is inserted into the form and it is used to do a repetitive job of generating the passwords.We create passwords generating procedure <strong>generate ()<\/strong> and it is called by the <strong>\u00a0Timer1_Tick()<\/strong> event so that the procedure is repeated after every interval.<\/p>\n<p>The interval of the timer can be set in its properties window. A value of 1 is 1 millisecond and a value of 1000 is 1 second. We shall set the Timer&#8217;s interval at 100 which is equivalent to 0.1 seconds. The Timer1.Enabled property is set to false so that the program will only start generating the passwords after the user\u00a0clicks on the Generate button. <strong>Rnd<\/strong> is a Visual Basic 2015 function that generates a random number between 0 and 1. Multiplying Rnd by 100 will produce\u00a0a number between 0 and 100. Int is a Visual Basic 2015 function that returns an integer by ignoring the decimal part of that number.<\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"4768455349\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nTherefore, Int(Rnd*100) will produce a number between 0 and 99, and the value of Int(Rnd*100)+100 will produce a number between 100 and 199.Finally, the program uses If\u2026Then\u2026Else to check whether the generated password is equal the actual password or not. If they are equal, the passwords generating process will be terminated by setting the Timer1.Enabled property to false.<\/p>\n<p><strong>The Code<\/strong><\/p>\n<pre style=\"font-size: 110%;\">Public Class Form1\r\nDim password As Integer Dim crackpass As Integer\r\n\r\nPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click\r\nTimer1.Enabled = True\r\nEnd Sub\r\n\r\nPrivate Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick\r\n\r\ngenerate()\r\n\r\nIf crackpass = password Then\r\n\r\nTimer1.Enabled = False\r\nLabel1.Text = crackpass\r\nMsgBox(\"Password Cracked!Login Successful!\")\r\nElse Label1.Text = crackpass\r\nLabel2.Text = \"Please wait...\"\r\n\r\nEnd If\r\n\r\nEnd Sub\r\n\r\nSub generate()\r\ncrackpass = Int(Rnd() * 100) + 100\r\nEnd Sub\r\n\r\n\r\nPrivate Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load\r\npassword = 123\r\nEnd Sub\r\nEnd Class<\/pre>\n<p><span style=\"line-height: 1.714285714; font-size: 1rem;\">The output<\/span><\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure16.2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4611\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure16.2.jpg\" alt=\"vb2013_figure16.2\" width=\"300\" height=\"300\" \/><\/a><\/p>\n<p align=\"center\"><strong>Figure 16.2: Password Generating P<\/strong><b>hase<\/b><\/p>\n<p><a href=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure16.3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4612\" src=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure16.3.jpg\" alt=\"vb2013_figure16.3\" width=\"256\" height=\"155\" \/><\/a><\/p>\n<p align=\"center\"><strong><span style=\"font-size: 1rem;\">Figure 16.3: Message Showing S<\/span>uccessful<span style=\"font-size: 1rem;\">\u00a0Login<\/span><\/strong><\/p>\n<p><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"5090773108\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 style=\"text-align: center;\"><a title=\"visual basic tutorial lesson 15\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-15-looping-repetitive-jobs\/\">[Lesson 15]<\/a> &lt;&lt; <a title=\"visual basic 2015 tutorial \" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-tutorial\/\">[Contents]<\/a> &gt;&gt; <a title=\"visual basic 2015 tutorial lesson 17\" href=\"http:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-17-functions\/\">[Lesson 17]<\/a><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Lesson 15] &lt;&lt; [Contents] &gt;&gt; [Lesson 17] A procedure is a program code that can carry out certain tasks or return a value. It can be called from other procedures. In Visual Basic 2015, there are two types of procedures; \u00a0sub procedures and functions. A sub procedure(also call subroutine) is a procedure that performs a &hellip; <a href=\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Visual Basic 2015 Lesson 16: Understanding Sub Procedures<\/span><\/a><\/p>\n","protected":false},"author":23013,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-6458","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB<\/title>\n<meta name=\"description\" content=\"This article describes how to write a sub procedure in visual basic 2015\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"og:description\" content=\"This article describes how to write a sub procedure in visual basic 2015\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html\" \/>\n<meta property=\"og:site_name\" content=\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Vbtutor\" \/>\n<meta property=\"article:modified_time\" content=\"2018-06-22T10:04:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@liewvk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/\",\"url\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html\",\"name\":\"Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"isPartOf\":{\"@id\":\"https:\/\/www.vbtutor.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage\"},\"image\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\",\"datePublished\":\"2015-04-06T03:11:31+00:00\",\"dateModified\":\"2018-06-22T10:04:54+00:00\",\"description\":\"This article describes how to write a sub procedure in visual basic 2015\",\"breadcrumb\":{\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage\",\"url\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\",\"contentUrl\":\"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg\",\"width\":154,\"height\":155},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vbtutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Visual Basic 2015 Lesson 16: Understanding Sub Procedures\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vbtutor.net\/#website\",\"url\":\"https:\/\/www.vbtutor.net\/\",\"name\":\"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB\",\"description\":\"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"This article describes how to write a sub procedure in visual basic 2015","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html","og_locale":"en_US","og_type":"article","og_title":"Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","og_description":"This article describes how to write a sub procedure in visual basic 2015","og_url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html","og_site_name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","article_publisher":"https:\/\/www.facebook.com\/Vbtutor","article_modified_time":"2018-06-22T10:04:54+00:00","og_image":[{"url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@liewvk","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vbtutor.net\/index.php\/visual-basic-2015-lesson-16-understanding-sub-procedures\/","url":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html","name":"Visual Basic 2015 Lesson 16: Understanding Sub Procedures - Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","isPartOf":{"@id":"https:\/\/www.vbtutor.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage"},"image":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage"},"thumbnailUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg","datePublished":"2015-04-06T03:11:31+00:00","dateModified":"2018-06-22T10:04:54+00:00","description":"This article describes how to write a sub procedure in visual basic 2015","breadcrumb":{"@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#primaryimage","url":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg","contentUrl":"https:\/\/www.vbtutor.net\/wordpress\/wp-content\/uploads\/2014\/01\/vb2013_figure11.1.jpg","width":154,"height":155},{"@type":"BreadcrumbList","@id":"http:\/\/www.vbtutor.net\/vb2015\/vb2015_lesson16.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vbtutor.net\/"},{"@type":"ListItem","position":2,"name":"Visual Basic 2015 Lesson 16: Understanding Sub Procedures"}]},{"@type":"WebSite","@id":"https:\/\/www.vbtutor.net\/#website","url":"https:\/\/www.vbtutor.net\/","name":"Learn Visual Basic Programming \u2013 VB.NET, VBA &amp; Classic VB","description":"Start learning Visual Basic from beginner to advanced. Includes VB.NET, VBA, and classic VB tutorials for students and professionals.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/users\/23013"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=6458"}],"version-history":[{"count":44,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6458\/revisions"}],"predecessor-version":[{"id":13032,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/pages\/6458\/revisions\/13032"}],"wp:attachment":[{"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=6458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/categories?post=6458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vbtutor.net\/index.php\/wp-json\/wp\/v2\/tags?post=6458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}