{"id":33,"date":"2014-01-24T17:01:22","date_gmt":"2014-01-25T00:01:22","guid":{"rendered":"https:\/\/blogs.ubc.ca\/coetoolbox\/?page_id=33"},"modified":"2016-10-04T11:30:46","modified_gmt":"2016-10-04T18:30:46","slug":"procedures","status":"publish","type":"page","link":"https:\/\/blogs.ubc.ca\/coetoolbox\/vba\/procedures\/","title":{"rendered":"Procedures and Functions"},"content":{"rendered":"<p>A procedure is a group of statements called and executed as a unit to perform a task. Note: a broader concept in programming language is a function, however we will refer to procedures here to avoid confusion with a <em>function<\/em> procedure in VBA.<\/p>\n<h2>1. Designing procedures<\/h2>\n<p>When coding\u00a0a procedure it is necessary to clearly know two things. First, what type of inputs and outputs must the function produce? This is called the <em>signature<\/em> of a\u00a0procedure:<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<span style=\"color: #0000ff;\">Input<\/span> DataType\u00a0<strong><span style=\"color: #800000;\">\u2192<\/span><\/strong> <span style=\"color: #0000ff;\">Output<\/span> DataType<\/p>\n<p>Secondly, what is\u00a0the <em>purpose<\/em> of the procedure?\u00a0\u00a0Always write it in a one-line description that you can include as a comment in your code. A good programming practice is\u00a0to define procedures that perform one specific task.<\/p>\n<h2>2. Sub and Function Procedures<\/h2>\n<p>In VBA there are two type of procedures: Subs (like sub-routines) and Functions.<\/p>\n<p><strong>Sub procedure<\/strong>: it does not return a value to the statement that called it, although it may perform other operations like modifying cells in a worksheet.<\/p>\n<p style=\"padding-left: 30px;\">Sub <span style=\"color: #0000ff;\">SubName<\/span> (arg)<br \/>\nstatements<br \/>\nEnd Sub<\/p>\n<p>To call a procedure within another procedure you just put the procedure name in a code line (with any necessary arguments), or you can use the Call statement:<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Call<\/span> SubName<\/p>\n<p><strong>Function procedure<\/strong>: this can return a value to the statement that called it, therefore you need to set the the output data type in the function declaration.<\/p>\n<p style=\"padding-left: 30px;\">Function <span style=\"color: #0000ff;\">FunctionName<\/span> (arg) as <span style=\"color: #0000ff;\">DataType<\/span><br \/>\nstatements<br \/>\n<span style=\"color: #0000ff;\">FunctionName<\/span> = <span style=\"color: #800000;\">value<\/span><br \/>\nEnd Function<\/p>\n<p>To set the value that the function should return, you have to define a variable with the same name as the function within the function body.\u00a0 To call the function you just set a variable with the same data type of the function.<\/p>\n<p style=\"padding-left: 30px;\">y = FunctionName(x)<\/p>\n<h2>3. Passing Arguments to Procedures<\/h2>\n<p>You can pass values to a procedure for processing within the procedure. These values are called arguments.<\/p>\n<p style=\"padding-left: 30px;\">Sub <span style=\"color: #800000;\">ProcedureName<\/span> (<span style=\"color: #0000ff;\">ArgName<\/span> as <span style=\"color: #0000ff;\">DataType<\/span>)<br \/>\nFunction <span style=\"color: #800000;\">FunctionName<\/span> (<span style=\"color: #0000ff;\">ArgName<\/span> as <span style=\"color: #0000ff;\">DataType<\/span>) as <span style=\"color: #800000;\">DataType<\/span><\/p>\n<p><strong>Optional Arguments<\/strong><\/p>\n<p>You can create procedures with optional arguments using the Optional statement.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Optional<\/span> <span style=\"color: #800000;\">ByVal<\/span> ArgName as DataType <span style=\"color: #0000ff;\">= DefaultValue<\/span><\/p>\n<p>You must also declare the default value and any arguments that follow an Optional argument must also be optional<\/p>\n<p><strong>Procedure Scope<\/strong><\/p>\n<ul>\n<li><span style=\"color: #0000ff;\">Public<\/span>: whole application (default)<\/li>\n<li><span style=\"color: #0000ff;\">Private<\/span>: only module<\/li>\n<\/ul>\n<p><strong>Early termination<\/strong><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Exit<\/span> Sub<br \/>\n<span style=\"color: #0000ff;\">Exit<\/span> Function<\/p>\n<h2>\u00a04. User Defined Math Functions<\/h2>\n<p>You can use Function procedures to create user-defined math functions, such as:<\/p>\n<p><strong>Log base n function<\/strong><\/p>\n<p>Function <span style=\"color: #0000ff;\">LogBaseN <\/span>(x as Double, n as Integer) as Double<br \/>\nLogBaseN = Log(x) \/ Log(n)<br \/>\nEnd Function<\/p>\n<p><strong>Poisson function<\/strong>: you can code a function that returns random numbers with a Poisson distribution using <a title=\"Generating Poisson-distributed random variables\" href=\"http:\/\/en.wikipedia.org\/wiki\/Poisson_distribution#Generating_Poisson-distributed_random_variables\" target=\"_blank\">Knuth&#8217;s algorithm<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A procedure is a group of statements called and executed as a unit to perform a task. Note: a broader concept in programming language is a function, however we will refer to procedures here to avoid confusion with a function procedure in VBA. 1. Designing procedures When coding\u00a0a procedure it is necessary to clearly know [&hellip;]<\/p>\n","protected":false},"author":22982,"featured_media":0,"parent":2,"menu_order":6,"comment_status":"open","ping_status":"closed","template":"page-templates\/full-width.php","meta":{"footnotes":""},"class_list":["post-33","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/users\/22982"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":19,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/33\/revisions"}],"predecessor-version":[{"id":1508,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/33\/revisions\/1508"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/coetoolbox\/wp-json\/wp\/v2\/media?parent=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}